dsh-seal-scripts 0.0.0-stage → 0.4.7

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.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +318 -0
  3. package/README.md +342 -2
  4. package/cordis.patch.yml +6 -0
  5. package/index.js +12 -0
  6. package/lib/client.js +1465 -0
  7. package/lib/core/assembly.js +321 -0
  8. package/lib/core/catalog-snapshot.js +78 -0
  9. package/lib/core/diary.js +100 -0
  10. package/lib/core/health.js +72 -0
  11. package/lib/core/index.js +491 -0
  12. package/lib/core/inject.js +146 -0
  13. package/lib/core/lang.js +251 -0
  14. package/lib/core/locale.js +56 -0
  15. package/lib/core/memory-file.js +214 -0
  16. package/lib/core/migrate.js +97 -0
  17. package/lib/core/path-guard.js +43 -0
  18. package/lib/core/project-key.js +52 -0
  19. package/lib/core/render.js +228 -0
  20. package/lib/core/schema-runner.js +281 -0
  21. package/lib/core/schema-tools.js +794 -0
  22. package/lib/core/schema.js +493 -0
  23. package/lib/core/search.js +254 -0
  24. package/lib/core/settings.js +158 -0
  25. package/lib/core/store.js +408 -0
  26. package/lib/core/tool-transfer.js +249 -0
  27. package/lib/core/tool.js +602 -0
  28. package/lib/core/zip.js +226 -0
  29. package/lib/core/zstd-js.js +705 -0
  30. package/lib/core/zstd.js +103 -0
  31. package/lib/host/api.js +9 -0
  32. package/lib/host/capabilities.js +155 -0
  33. package/lib/host/compact-delete.js +76 -0
  34. package/lib/host/delete.js +136 -0
  35. package/lib/host/host-contract.js +73 -0
  36. package/lib/host/http-util.js +82 -0
  37. package/lib/host/lifecycle.js +66 -0
  38. package/lib/host/register.js +63 -0
  39. package/lib/host/routes/memories.js +102 -0
  40. package/lib/host/routes/options.js +46 -0
  41. package/lib/host/routes/sessions.js +194 -0
  42. package/lib/host/routes/tools.js +103 -0
  43. package/lib/host/summary.js +71 -0
  44. package/package.json +60 -4
@@ -0,0 +1,226 @@
1
+ /**
2
+ * 极简 zip 读写(0.4.3):「自创工具」页的「导入 / 导出」用它。
3
+ *
4
+ * 写:只用 store(压缩方法 0,压缩率 0)。导出的包要能被任何解压工具直接打开,
5
+ * 而 toolScript 本身就是小文本,压不压差不了多少——不引第三方依赖,也不冒
6
+ * deflate 实现不一致的风险。
7
+ * 读:store(0) 与 deflate(8) 都认,别人的 zip 也能导进来;别的压缩方法明确报错。
8
+ *
9
+ * 只依赖 node 内置模块(node:zlib)。
10
+ */
11
+
12
+ import { inflateRawSync } from 'node:zlib'
13
+
14
+ const SIG_LOCAL = 0x04034b50
15
+ const SIG_CENTRAL = 0x02014b50
16
+ const SIG_EOCD = 0x06054b50
17
+
18
+ /** 通用标志位 bit 11:文件名是 UTF-8。写包时一直置上。 */
19
+ const FLAG_UTF8 = 0x0800
20
+
21
+ /** 包内单个文件上限 / 解压后合计上限。 */
22
+ export const MAX_ZIP_ENTRY_BYTES = 64 * 1024 * 1024
23
+ export const MAX_ZIP_TOTAL_BYTES = 128 * 1024 * 1024
24
+
25
+ let CRC_TABLE = null
26
+
27
+ /** CRC32 查表(zip 头必须填)。 */
28
+ function crcTable() {
29
+ if (CRC_TABLE !== null) return CRC_TABLE
30
+ const table = new Int32Array(256)
31
+ for (let n = 0; n < 256; n++) {
32
+ let c = n
33
+ for (let k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)
34
+ table[n] = c
35
+ }
36
+ CRC_TABLE = table
37
+ return table
38
+ }
39
+
40
+ /**
41
+ * @param {Buffer|Uint8Array} buf
42
+ * @returns {number} 无符号 32 位 CRC
43
+ */
44
+ export function crc32(buf) {
45
+ const table = crcTable()
46
+ let c = -1
47
+ for (let i = 0; i < buf.length; i++) c = table[(c ^ buf[i]) & 0xFF] ^ (c >>> 8)
48
+ return (c ^ -1) >>> 0
49
+ }
50
+
51
+ /** Date → DOS 时间/日期(zip 固定格式)。 */
52
+ function dosDateTime(date) {
53
+ const d = date instanceof Date && Number.isFinite(date.getTime()) ? date : new Date()
54
+ const year = Math.max(1980, d.getFullYear())
55
+ const time = ((d.getHours() & 0x1F) << 11) | ((d.getMinutes() & 0x3F) << 5) | ((Math.floor(d.getSeconds() / 2)) & 0x1F)
56
+ const day = (((year - 1980) & 0x7F) << 9) | (((d.getMonth() + 1) & 0x0F) << 5) | (d.getDate() & 0x1F)
57
+ return { time: time & 0xFFFF, date: day & 0xFFFF }
58
+ }
59
+
60
+ /**
61
+ * 打一个 zip 包(全部 store,压缩率 0)。
62
+ *
63
+ * @param {Array<{name:string, data?:Buffer|string, mtime?:Date}>} entries 用 / 分隔;目录条目以 / 结尾
64
+ * @returns {Buffer}
65
+ */
66
+ export function buildZip(entries) {
67
+ const locals = []
68
+ const centrals = []
69
+ let offset = 0
70
+
71
+ for (const entry of entries) {
72
+ const name = String(entry.name).split('\\').join('/')
73
+ if (name === '') throw new Error('zip 条目名不能为空')
74
+ const isDir = name.endsWith('/')
75
+ const body = isDir
76
+ ? Buffer.alloc(0)
77
+ : (Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(String(entry.data === undefined ? '' : entry.data), 'utf8'))
78
+ const nameBuf = Buffer.from(name, 'utf8')
79
+ if (body.length > MAX_ZIP_ENTRY_BYTES) throw new Error('单个文件超过上限:' + name)
80
+ const crc = crc32(body)
81
+ const stamp = dosDateTime(entry.mtime)
82
+
83
+ const local = Buffer.alloc(30)
84
+ local.writeUInt32LE(SIG_LOCAL, 0)
85
+ local.writeUInt16LE(20, 4)
86
+ local.writeUInt16LE(FLAG_UTF8, 6)
87
+ local.writeUInt16LE(0, 8)
88
+ local.writeUInt16LE(stamp.time, 10)
89
+ local.writeUInt16LE(stamp.date, 12)
90
+ local.writeUInt32LE(crc, 14)
91
+ local.writeUInt32LE(body.length, 18)
92
+ local.writeUInt32LE(body.length, 22)
93
+ local.writeUInt16LE(nameBuf.length, 26)
94
+ local.writeUInt16LE(0, 28)
95
+ locals.push(local, nameBuf, body)
96
+
97
+ const central = Buffer.alloc(46)
98
+ central.writeUInt32LE(SIG_CENTRAL, 0)
99
+ central.writeUInt16LE(20, 4)
100
+ central.writeUInt16LE(20, 6)
101
+ central.writeUInt16LE(FLAG_UTF8, 8)
102
+ central.writeUInt16LE(0, 10)
103
+ central.writeUInt16LE(stamp.time, 12)
104
+ central.writeUInt16LE(stamp.date, 14)
105
+ central.writeUInt32LE(crc, 16)
106
+ central.writeUInt32LE(body.length, 20)
107
+ central.writeUInt32LE(body.length, 24)
108
+ central.writeUInt16LE(nameBuf.length, 28)
109
+ central.writeUInt16LE(0, 30)
110
+ central.writeUInt16LE(0, 32)
111
+ central.writeUInt16LE(0, 34)
112
+ central.writeUInt16LE(0, 36)
113
+ central.writeUInt32LE(isDir ? 0x10 : 0, 38)
114
+ central.writeUInt32LE(offset, 42)
115
+ centrals.push(central, nameBuf)
116
+
117
+ offset += local.length + nameBuf.length + body.length
118
+ }
119
+
120
+ const centralBuf = Buffer.concat(centrals)
121
+ const eocd = Buffer.alloc(22)
122
+ eocd.writeUInt32LE(SIG_EOCD, 0)
123
+ eocd.writeUInt16LE(0, 4)
124
+ eocd.writeUInt16LE(0, 6)
125
+ eocd.writeUInt16LE(entries.length, 8)
126
+ eocd.writeUInt16LE(entries.length, 10)
127
+ eocd.writeUInt32LE(centralBuf.length, 12)
128
+ eocd.writeUInt32LE(offset, 16)
129
+ eocd.writeUInt16LE(0, 20)
130
+ return Buffer.concat([...locals, centralBuf, eocd])
131
+ }
132
+
133
+ /** 从后往前找 EOCD(注释最长 65535 字节)。 */
134
+ function findEocd(buf) {
135
+ const min = Math.max(0, buf.length - 22 - 0xFFFF)
136
+ for (let i = buf.length - 22; i >= min; i--) {
137
+ if (buf.readUInt32LE(i) === SIG_EOCD) return i
138
+ }
139
+ return -1
140
+ }
141
+
142
+ /**
143
+ * 解开一个 zip。
144
+ *
145
+ * @param {Buffer} buf
146
+ * @returns {Array<{name:string, isDir:boolean, data:Buffer, method:number, crc:number}>}
147
+ */
148
+ export function readZip(buf) {
149
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
150
+ if (buf.length < 22) throw new Error('不是 zip:文件太短')
151
+ const eocd = findEocd(buf)
152
+ if (eocd < 0) throw new Error('不是 zip:找不到中央目录(可能只是改了扩展名的普通文件)')
153
+ const count = buf.readUInt16LE(eocd + 10)
154
+ const cdSize = buf.readUInt32LE(eocd + 12)
155
+ const cdOffset = buf.readUInt32LE(eocd + 16)
156
+ if (cdOffset === 0xFFFFFFFF || cdSize === 0xFFFFFFFF || count === 0xFFFF) {
157
+ throw new Error('不支持 zip64 包;重新导出一个普通 zip 再试')
158
+ }
159
+ if (cdOffset + cdSize > buf.length) throw new Error('zip 损坏:中央目录越界')
160
+
161
+ const out = []
162
+ let total = 0
163
+ let p = cdOffset
164
+ for (let n = 0; n < count; n++) {
165
+ if (p + 46 > buf.length || buf.readUInt32LE(p) !== SIG_CENTRAL) {
166
+ throw new Error('zip 损坏:中央目录第 ' + (n + 1) + ' 项读不出来')
167
+ }
168
+ const flags = buf.readUInt16LE(p + 8)
169
+ const method = buf.readUInt16LE(p + 10)
170
+ const crc = buf.readUInt32LE(p + 16)
171
+ const compSize = buf.readUInt32LE(p + 20)
172
+ const uncompSize = buf.readUInt32LE(p + 24)
173
+ const nameLen = buf.readUInt16LE(p + 28)
174
+ const extraLen = buf.readUInt16LE(p + 30)
175
+ const commentLen = buf.readUInt16LE(p + 32)
176
+ const localOffset = buf.readUInt32LE(p + 42)
177
+ const name = buf.subarray(p + 46, p + 46 + nameLen).toString('utf8').split('\\').join('/')
178
+ p += 46 + nameLen + extraLen + commentLen
179
+
180
+ if (compSize === 0xFFFFFFFF || uncompSize === 0xFFFFFFFF) throw new Error('不支持 zip64 包:' + name)
181
+ if ((flags & 0x01) === 0x01) throw new Error('不支持加密的 zip:' + name)
182
+
183
+ const isDir = name.endsWith('/')
184
+ if (isDir) { out.push({ name, isDir: true, data: Buffer.alloc(0), method, crc }); continue }
185
+
186
+ if (localOffset + 30 > buf.length || buf.readUInt32LE(localOffset) !== SIG_LOCAL) {
187
+ throw new Error('zip 损坏:找不到 ' + name + ' 的本地头')
188
+ }
189
+ const lNameLen = buf.readUInt16LE(localOffset + 26)
190
+ const lExtraLen = buf.readUInt16LE(localOffset + 28)
191
+ const dataStart = localOffset + 30 + lNameLen + lExtraLen
192
+ if (dataStart + compSize > buf.length) throw new Error('zip 损坏:' + name + ' 的数据越界')
193
+ const raw = buf.subarray(dataStart, dataStart + compSize)
194
+
195
+ let data
196
+ if (method === 0) data = Buffer.from(raw)
197
+ else if (method === 8) data = inflateRawSync(raw)
198
+ else throw new Error('不支持的压缩方法 ' + method + '(' + name + ');只认 store / deflate')
199
+
200
+ if (data.length > MAX_ZIP_ENTRY_BYTES) throw new Error('包内文件超过上限:' + name)
201
+ total += data.length
202
+ if (total > MAX_ZIP_TOTAL_BYTES) throw new Error('解压后合计超过上限(' + MAX_ZIP_TOTAL_BYTES + ' 字节)')
203
+ if (data.length !== uncompSize) throw new Error('zip 损坏:' + name + ' 解出来的长度对不上')
204
+ out.push({ name, isDir: false, data, method, crc })
205
+ }
206
+ return out
207
+ }
208
+
209
+ /**
210
+ * 把包内条目名收成干净相对路径:不许绝对路径、不许 ..、不许盘符。
211
+ * 越界直接抛——导入是把别人的文件写进本机工具目录,这一步不能只做「尽量」。
212
+ *
213
+ * @param {string} raw
214
+ * @returns {string} 规整后的相对路径(用 / 分隔,不含结尾 /)
215
+ */
216
+ export function safeEntryName(raw) {
217
+ const text = String(raw === undefined || raw === null ? '' : raw).split('\\').join('/').replace(/\/+$/, '')
218
+ if (text === '') throw new Error('包里有空文件名的条目')
219
+ if (text.startsWith('/')) throw new Error('包里有绝对路径:' + raw)
220
+ if (/^[A-Za-z]:/.test(text)) throw new Error('包里有盘符路径:' + raw)
221
+ const parts = text.split('/')
222
+ for (const part of parts) {
223
+ if (part === '' || part === '.' || part === '..') throw new Error('包里有越界路径:' + raw)
224
+ }
225
+ return parts.join('/')
226
+ }