dsh-seal-scripts 0.5.1 → 0.5.2
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/README.en.md +3 -1
- package/README.md +3 -1
- package/lib/client.js +784 -3
- package/lib/core/assembly.js +8 -8
- package/lib/core/host-limits.js +5 -5
- package/lib/core/index.js +30 -30
- package/lib/core/inject.js +1 -1
- package/lib/core/lang.js +418 -75
- package/lib/core/locale.js +1 -1
- package/lib/core/memory-file.js +2 -5
- package/lib/core/migrate.js +1 -1
- package/lib/core/path-guard.js +4 -4
- package/lib/core/schema-runner.js +10 -10
- package/lib/core/schema-tools.js +88 -88
- package/lib/core/schema.js +49 -61
- package/lib/core/search.js +1 -1
- package/lib/core/settings.js +1 -1
- package/lib/core/store.js +1 -1
- package/lib/core/tool-transfer.js +14 -14
- package/lib/core/tool.js +71 -71
- package/lib/core/zip.js +35 -35
- package/lib/core/zstd-js.js +50 -50
- package/lib/core/zstd.js +1 -1
- package/lib/host/capabilities.js +3 -3
- package/lib/host/compact-delete.js +11 -11
- package/lib/host/delete.js +16 -16
- package/lib/host/host-contract.js +2 -2
- package/lib/host/http-util.js +1 -1
- package/lib/host/lifecycle.js +2 -2
- package/lib/host/register.js +3 -3
- package/lib/host/routes/memories.js +7 -7
- package/lib/host/routes/sessions.js +8 -8
- package/lib/host/routes/tools.js +6 -6
- package/package.json +1 -1
package/lib/core/zstd-js.js
CHANGED
|
@@ -72,9 +72,9 @@ class ForwardBitReader {
|
|
|
72
72
|
|
|
73
73
|
class BackwardBitReader {
|
|
74
74
|
constructor(buf) {
|
|
75
|
-
if (buf.length === 0) throw fail('
|
|
75
|
+
if (buf.length === 0) throw fail('empty bit stream')
|
|
76
76
|
const last = buf[buf.length - 1]
|
|
77
|
-
if (last === 0) throw fail('
|
|
77
|
+
if (last === 0) throw fail('last byte of the bit stream is 0 (no end marker)')
|
|
78
78
|
const hb = highbit32(last)
|
|
79
79
|
// 最后一个有用位:末字节最高置位(结束标记)的前一位。
|
|
80
80
|
this.bitPos = (buf.length - 1) * 8 + hb - 1
|
|
@@ -127,7 +127,7 @@ function readFseTable(data, pos, maxAccuracyLog, expectedSymbols) {
|
|
|
127
127
|
const bits = new ForwardBitReader(data, pos)
|
|
128
128
|
const accuracyLog = bits.peek(4) + 5
|
|
129
129
|
bits.skip(4)
|
|
130
|
-
if (accuracyLog > maxAccuracyLog) throw fail('FSE accuracyLog
|
|
130
|
+
if (accuracyLog > maxAccuracyLog) throw fail('FSE accuracyLog out of range: ' + accuracyLog + ' > ' + maxAccuracyLog)
|
|
131
131
|
|
|
132
132
|
const tableSize = 1 << accuracyLog
|
|
133
133
|
// 每个符号的归一化概率;没出现的符号保持 0(零重复就是跳过这些槽位)。
|
|
@@ -169,8 +169,8 @@ function readFseTable(data, pos, maxAccuracyLog, expectedSymbols) {
|
|
|
169
169
|
} while (rep === 3)
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
|
-
if (probs !== 0) throw fail('FSE
|
|
173
|
-
if (sym >= expectedSymbols) throw fail('FSE
|
|
172
|
+
if (probs !== 0) throw fail('FSE distribution table not fully consumed: probs=' + probs)
|
|
173
|
+
if (sym >= expectedSymbols) throw fail('FSE distribution table has more symbols than the cap ' + expectedSymbols)
|
|
174
174
|
|
|
175
175
|
return { table: buildFseTable(counts, accuracyLog), bytes: bits.bytesUsed() }
|
|
176
176
|
}
|
|
@@ -201,12 +201,12 @@ function buildFseTable(counts, accuracyLog) {
|
|
|
201
201
|
while (position > highThreshold) position = (position + step) & mask
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
|
-
if (position !== 0) throw fail('FSE
|
|
204
|
+
if (position !== 0) throw fail('FSE table spread did not return to 0')
|
|
205
205
|
|
|
206
206
|
const cells = new Array(tableSize)
|
|
207
207
|
for (let u = 0; u < tableSize; u++) {
|
|
208
208
|
const symbol = symbols[u]
|
|
209
|
-
if (symbol < 0) throw fail('FSE
|
|
209
|
+
if (symbol < 0) throw fail('FSE table has holes')
|
|
210
210
|
const nextState = symbolNext[symbol]++
|
|
211
211
|
const nbBits = accuracyLog - highbit32(nextState)
|
|
212
212
|
cells[u] = { symbol, nbBits, newState: (nextState << nbBits) - tableSize }
|
|
@@ -250,7 +250,7 @@ for (let i = 0; i < ML_REST.length; i++) { ML_BASE[32 + i] = ML_REST[i][0]; ML_B
|
|
|
250
250
|
function decodeFseWeights(data) {
|
|
251
251
|
const { table, bytes } = readFseTable(data, 0, 6, 256)
|
|
252
252
|
const stream = data.subarray(bytes)
|
|
253
|
-
if (stream.length === 0) throw fail('Huffman weight
|
|
253
|
+
if (stream.length === 0) throw fail('Huffman weight bit stream is empty')
|
|
254
254
|
const bits = new BackwardBitReader(stream)
|
|
255
255
|
let s1 = bits.readBits(table.accuracyLog)
|
|
256
256
|
let s2 = bits.readBits(table.accuracyLog)
|
|
@@ -259,13 +259,13 @@ function decodeFseWeights(data) {
|
|
|
259
259
|
|
|
260
260
|
const advance = (state) => {
|
|
261
261
|
const cell = table.cells[state]
|
|
262
|
-
if (cell === undefined) throw fail('FSE
|
|
262
|
+
if (cell === undefined) throw fail('FSE state out of range')
|
|
263
263
|
const { value, overflow } = bits.readBitsLenient(cell.nbBits)
|
|
264
264
|
return { state: cell.newState + value, overflow }
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
for (;;) {
|
|
268
|
-
if (out.length > 255) throw fail('Huffman
|
|
268
|
+
if (out.length > 255) throw fail('too many Huffman weights')
|
|
269
269
|
if (first) {
|
|
270
270
|
out.push(table.cells[s1].symbol)
|
|
271
271
|
const r = advance(s1)
|
|
@@ -285,7 +285,7 @@ function decodeFseWeights(data) {
|
|
|
285
285
|
/** 读 Huffman 树描述,返回 { tree, bytes }。 */
|
|
286
286
|
function readHuffmanTree(data, pos) {
|
|
287
287
|
const header = data[pos]
|
|
288
|
-
if (header === undefined) throw fail('Huffman
|
|
288
|
+
if (header === undefined) throw fail('Huffman tree description truncated')
|
|
289
289
|
let weights = []
|
|
290
290
|
let bytes
|
|
291
291
|
if (header >= 128) {
|
|
@@ -294,14 +294,14 @@ function readHuffmanTree(data, pos) {
|
|
|
294
294
|
const start = pos + 1
|
|
295
295
|
for (let i = 0; i < n; i++) {
|
|
296
296
|
const b = data[start + (i >> 1)]
|
|
297
|
-
if (b === undefined) throw fail('Huffman weight
|
|
297
|
+
if (b === undefined) throw fail('Huffman weight truncated')
|
|
298
298
|
weights.push(i % 2 === 0 ? (b >> 4) : (b & 0x0f))
|
|
299
299
|
}
|
|
300
300
|
} else {
|
|
301
|
-
if (header === 0) throw fail('Huffman
|
|
301
|
+
if (header === 0) throw fail('Huffman tree description length is 0')
|
|
302
302
|
bytes = 1 + header
|
|
303
303
|
const segment = data.subarray(pos + 1, pos + 1 + header)
|
|
304
|
-
if (segment.length !== header) throw fail('Huffman
|
|
304
|
+
if (segment.length !== header) throw fail('Huffman tree description truncated')
|
|
305
305
|
weights = decodeFseWeights(segment)
|
|
306
306
|
}
|
|
307
307
|
|
|
@@ -309,14 +309,14 @@ function readHuffmanTree(data, pos) {
|
|
|
309
309
|
for (const w of weights) {
|
|
310
310
|
if (w > 0) sum += 2 ** (w - 1)
|
|
311
311
|
}
|
|
312
|
-
if (sum === 0) throw fail('Huffman
|
|
312
|
+
if (sum === 0) throw fail('all Huffman weights are 0')
|
|
313
313
|
const maxBits = Math.floor(Math.log2(sum)) + 1
|
|
314
314
|
// 最后一个 weight 的“贡献”是 2^(w-1) = tableSize - sum;所以先要求它是 2 的幂,
|
|
315
315
|
// 再取 w = log2(rem) + 1(RFC 例子里 rem=1,看起来像直接相减,其实不是)。
|
|
316
316
|
const rem = 2 ** maxBits - sum
|
|
317
|
-
if (rem <= 0 || (rem & (rem - 1)) !== 0) throw fail('Huffman
|
|
317
|
+
if (rem <= 0 || (rem & (rem - 1)) !== 0) throw fail('Huffman last remaining sum is not a power of two: ' + rem)
|
|
318
318
|
const lastWeight = Math.log2(rem) + 1
|
|
319
|
-
if (lastWeight > maxBits) throw fail('Huffman
|
|
319
|
+
if (lastWeight > maxBits) throw fail('invalid last Huffman weight: ' + lastWeight)
|
|
320
320
|
weights.push(lastWeight)
|
|
321
321
|
|
|
322
322
|
const lengths = weights.map((w) => (w === 0 ? 0 : maxBits + 1 - w))
|
|
@@ -346,7 +346,7 @@ function decodeHuffmanStream(data, count, tree) {
|
|
|
346
346
|
const hit = tree.codes[len].get(code)
|
|
347
347
|
if (hit !== undefined) { sym = hit; break }
|
|
348
348
|
}
|
|
349
|
-
if (sym < 0) throw fail('Huffman
|
|
349
|
+
if (sym < 0) throw fail('Huffman code cannot be matched')
|
|
350
350
|
out[i] = sym
|
|
351
351
|
}
|
|
352
352
|
return out
|
|
@@ -359,7 +359,7 @@ function decodeHuffmanStream(data, count, tree) {
|
|
|
359
359
|
/** 读 literals section,返回 { literals, end }。 */
|
|
360
360
|
function readLiterals(block, p, ctx) {
|
|
361
361
|
const b0 = block[p]
|
|
362
|
-
if (b0 === undefined) throw fail('literals
|
|
362
|
+
if (b0 === undefined) throw fail('literals header truncated')
|
|
363
363
|
const type = b0 & 3
|
|
364
364
|
const sf = (b0 >> 2) & 3
|
|
365
365
|
|
|
@@ -381,7 +381,7 @@ function readLiterals(block, p, ctx) {
|
|
|
381
381
|
return { literals: Buffer.alloc(regen, block[p + headerSize] ?? 0), end: p + headerSize + 1 }
|
|
382
382
|
}
|
|
383
383
|
const start = p + headerSize
|
|
384
|
-
if (start + regen > block.length) throw fail('raw literals
|
|
384
|
+
if (start + regen > block.length) throw fail('raw literals out of range')
|
|
385
385
|
return { literals: block.subarray(start, start + regen), end: start + regen }
|
|
386
386
|
}
|
|
387
387
|
|
|
@@ -402,12 +402,12 @@ function readLiterals(block, p, ctx) {
|
|
|
402
402
|
treeBytes = parsed.bytes
|
|
403
403
|
ctx.huffman = tree
|
|
404
404
|
} else if (!tree) {
|
|
405
|
-
throw fail('treeless literals
|
|
405
|
+
throw fail('treeless literals but no previous Huffman tree')
|
|
406
406
|
}
|
|
407
407
|
|
|
408
408
|
const streamsStart = p + headerSize + treeBytes
|
|
409
409
|
const totalStreams = compSize - treeBytes
|
|
410
|
-
if (totalStreams < 0) throw fail('Huffman
|
|
410
|
+
if (totalStreams < 0) throw fail('invalid Huffman stream length')
|
|
411
411
|
|
|
412
412
|
if (sf === 0) {
|
|
413
413
|
const data = block.subarray(streamsStart, streamsStart + totalStreams)
|
|
@@ -416,23 +416,23 @@ function readLiterals(block, p, ctx) {
|
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
// 4 个流:前 3 个长度在 jump table 里
|
|
419
|
-
if (totalStreams < 6) throw fail('jump table
|
|
419
|
+
if (totalStreams < 6) throw fail('jump table truncated')
|
|
420
420
|
const jump = streamsStart
|
|
421
421
|
const s1 = block.readUInt16LE(jump)
|
|
422
422
|
const s2 = block.readUInt16LE(jump + 2)
|
|
423
423
|
const s3 = block.readUInt16LE(jump + 4)
|
|
424
424
|
const s4 = totalStreams - 6 - s1 - s2 - s3
|
|
425
|
-
if (s4 < 0) throw fail('Huffman
|
|
425
|
+
if (s4 < 0) throw fail('invalid Huffman stream length (jump table)')
|
|
426
426
|
const sizes = [s1, s2, s3, s4]
|
|
427
427
|
const segment = Math.ceil(regen / 4)
|
|
428
428
|
const regenSizes = [segment, segment, segment, regen - 3 * segment]
|
|
429
|
-
if (regenSizes[3] < 0) throw fail('4
|
|
429
|
+
if (regenSizes[3] < 0) throw fail('4-stream literals too small')
|
|
430
430
|
const literals = Buffer.allocUnsafe(regen)
|
|
431
431
|
let cursor = jump + 6
|
|
432
432
|
let outPos = 0
|
|
433
433
|
for (let i = 0; i < 4; i++) {
|
|
434
434
|
const data = block.subarray(cursor, cursor + sizes[i])
|
|
435
|
-
if (data.length !== sizes[i]) throw fail('Huffman
|
|
435
|
+
if (data.length !== sizes[i]) throw fail('Huffman stream truncated')
|
|
436
436
|
const part = decodeHuffmanStream(data, regenSizes[i], tree)
|
|
437
437
|
part.copy(literals, outPos)
|
|
438
438
|
outPos += part.length
|
|
@@ -449,7 +449,7 @@ function initState(bits, table) {
|
|
|
449
449
|
/** 更新一个状态,返回 { state, overflow }。 */
|
|
450
450
|
function stepState(bits, table, state) {
|
|
451
451
|
const cell = table.cells[state]
|
|
452
|
-
if (cell === undefined) throw fail('FSE
|
|
452
|
+
if (cell === undefined) throw fail('FSE state out of range')
|
|
453
453
|
const { value, overflow } = bits.readBitsLenient(cell.nbBits)
|
|
454
454
|
return { state: cell.newState + value, overflow }
|
|
455
455
|
}
|
|
@@ -457,7 +457,7 @@ function stepState(bits, table, state) {
|
|
|
457
457
|
/** 解 sequences 段并把结果写进输出缓冲区。 */
|
|
458
458
|
function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
459
459
|
let p = seqStart
|
|
460
|
-
if (p >= block.length) throw fail('
|
|
460
|
+
if (p >= block.length) throw fail('missing sequences header')
|
|
461
461
|
const b0 = block[p++]
|
|
462
462
|
let count
|
|
463
463
|
if (b0 === 0) {
|
|
@@ -474,21 +474,21 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
474
474
|
}
|
|
475
475
|
|
|
476
476
|
const modes = block[p++]
|
|
477
|
-
if (modes === undefined) throw fail('
|
|
478
|
-
if ((modes & 3) !== 0) throw fail('symbol compression modes
|
|
477
|
+
if (modes === undefined) throw fail('missing symbol compression modes')
|
|
478
|
+
if ((modes & 3) !== 0) throw fail('symbol compression modes reserved bits are not 0')
|
|
479
479
|
const modeLL = (modes >> 6) & 3
|
|
480
480
|
const modeOF = (modes >> 4) & 3
|
|
481
481
|
const modeML = (modes >> 2) & 3
|
|
482
482
|
|
|
483
483
|
const readTable = (mode, key, maxLog, symbols) => {
|
|
484
484
|
if (mode === 3) {
|
|
485
|
-
if (!ctx[key]) throw fail('repeat
|
|
485
|
+
if (!ctx[key]) throw fail('repeat mode has no previous ' + key + ' table')
|
|
486
486
|
return ctx[key]
|
|
487
487
|
}
|
|
488
488
|
if (mode === 0) return PREDEFINED[key]
|
|
489
489
|
if (mode === 1) {
|
|
490
490
|
const symbol = block[p++]
|
|
491
|
-
if (symbol === undefined) throw fail('RLE
|
|
491
|
+
if (symbol === undefined) throw fail('RLE table is missing a symbol')
|
|
492
492
|
return rleTable(symbol)
|
|
493
493
|
}
|
|
494
494
|
const parsed = readFseTable(block, p, maxLog, symbols)
|
|
@@ -514,7 +514,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
514
514
|
for (let i = 0; i < count; i++) {
|
|
515
515
|
const cellOF = tableOF.cells[stateOF]
|
|
516
516
|
const offsetCode = cellOF.symbol
|
|
517
|
-
if (offsetCode > 31) throw fail('offset code
|
|
517
|
+
if (offsetCode > 31) throw fail('offset code out of range')
|
|
518
518
|
const offsetValue = 2 ** offsetCode + bits.readBits(offsetCode)
|
|
519
519
|
|
|
520
520
|
const cellML = tableML.cells[stateML]
|
|
@@ -525,7 +525,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
525
525
|
const llCode = cellLL.symbol
|
|
526
526
|
const literalLength = LL_BASE[llCode] + bits.readBits(LL_BITS[llCode])
|
|
527
527
|
|
|
528
|
-
if (litPos + literalLength > literals.length) throw fail('literal
|
|
528
|
+
if (litPos + literalLength > literals.length) throw fail('literal length out of range')
|
|
529
529
|
out.pushBuffer(literals.subarray(litPos, litPos + literalLength))
|
|
530
530
|
litPos += literalLength
|
|
531
531
|
|
|
@@ -546,7 +546,7 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
546
546
|
rep[0] = offset
|
|
547
547
|
}
|
|
548
548
|
}
|
|
549
|
-
if (!Number.isInteger(offset) || offset <= 0 || offset > out.pos) throw fail('match offset
|
|
549
|
+
if (!Number.isInteger(offset) || offset <= 0 || offset > out.pos) throw fail('match offset out of range: ' + offset)
|
|
550
550
|
for (let k = 0; k < matchLength; k++) {
|
|
551
551
|
out.ensure(1)
|
|
552
552
|
out.buf[out.pos] = out.buf[out.pos - offset]
|
|
@@ -555,11 +555,11 @@ function decodeSequences(block, seqStart, literals, ctx, out) {
|
|
|
555
555
|
|
|
556
556
|
if (i + 1 < count) {
|
|
557
557
|
const sLL = stepState(bits, tableLL, stateLL); stateLL = sLL.state
|
|
558
|
-
if (sLL.overflow) throw fail('sequences
|
|
558
|
+
if (sLL.overflow) throw fail('sequences bit stream ran out early')
|
|
559
559
|
const sML = stepState(bits, tableML, stateML); stateML = sML.state
|
|
560
|
-
if (sML.overflow) throw fail('sequences
|
|
560
|
+
if (sML.overflow) throw fail('sequences bit stream ran out early')
|
|
561
561
|
const sOF = stepState(bits, tableOF, stateOF); stateOF = sOF.state
|
|
562
|
-
if (sOF.overflow) throw fail('sequences
|
|
562
|
+
if (sOF.overflow) throw fail('sequences bit stream ran out early')
|
|
563
563
|
}
|
|
564
564
|
}
|
|
565
565
|
|
|
@@ -606,18 +606,18 @@ class OutputBuffer {
|
|
|
606
606
|
/** 解一帧,返回 { output, length }。 */
|
|
607
607
|
function decodeFrame(buf, start) {
|
|
608
608
|
let p = start + 4
|
|
609
|
-
if (p >= buf.length) throw fail('
|
|
609
|
+
if (p >= buf.length) throw fail('frame header truncated')
|
|
610
610
|
const fhd = buf[p++]
|
|
611
611
|
const fcsFlag = fhd >> 6
|
|
612
612
|
const single = (fhd >> 5) & 1
|
|
613
613
|
const checksum = (fhd >> 2) & 1
|
|
614
614
|
const dictFlag = fhd & 3
|
|
615
|
-
if (((fhd >> 3) & 1) === 1) throw fail('
|
|
615
|
+
if (((fhd >> 3) & 1) === 1) throw fail('reserved bits are not 0')
|
|
616
616
|
|
|
617
617
|
let windowSize = 0
|
|
618
618
|
if (!single) {
|
|
619
619
|
const wd = buf[p++]
|
|
620
|
-
if (wd === undefined) throw fail('
|
|
620
|
+
if (wd === undefined) throw fail('window descriptor truncated')
|
|
621
621
|
const exp = wd >> 3
|
|
622
622
|
const mant = wd & 7
|
|
623
623
|
windowSize = 2 ** (10 + exp) + (2 ** (10 + exp) / 8) * mant
|
|
@@ -627,7 +627,7 @@ function decodeFrame(buf, start) {
|
|
|
627
627
|
let dictId = 0
|
|
628
628
|
for (let i = 0; i < dictSize; i++) dictId += (buf[p + i] ?? 0) * (256 ** i)
|
|
629
629
|
p += dictSize
|
|
630
|
-
if (dictId !== 0) throw fail('
|
|
630
|
+
if (dictId !== 0) throw fail('frames with a dictionary are not supported (dictID=' + dictId + ')', 'zstd-unsupported')
|
|
631
631
|
|
|
632
632
|
let fcsSize = [0, 2, 4, 8][fcsFlag]
|
|
633
633
|
if (fcsFlag === 0 && single) fcsSize = 1
|
|
@@ -651,7 +651,7 @@ function decodeFrame(buf, start) {
|
|
|
651
651
|
|
|
652
652
|
let last = false
|
|
653
653
|
while (!last) {
|
|
654
|
-
if (p + 3 > buf.length) throw fail('block header
|
|
654
|
+
if (p + 3 > buf.length) throw fail('block header truncated')
|
|
655
655
|
const h = buf[p] | (buf[p + 1] << 8) | (buf[p + 2] << 16)
|
|
656
656
|
p += 3
|
|
657
657
|
last = (h & 1) === 1
|
|
@@ -659,20 +659,20 @@ function decodeFrame(buf, start) {
|
|
|
659
659
|
const size = h >>> 3
|
|
660
660
|
|
|
661
661
|
if (type === 0) {
|
|
662
|
-
if (p + size > buf.length) throw fail('raw block
|
|
662
|
+
if (p + size > buf.length) throw fail('raw block truncated')
|
|
663
663
|
out.pushBuffer(buf.subarray(p, p + size))
|
|
664
664
|
p += size
|
|
665
665
|
} else if (type === 1) {
|
|
666
|
-
if (p >= buf.length) throw fail('RLE block
|
|
666
|
+
if (p >= buf.length) throw fail('RLE block truncated')
|
|
667
667
|
out.pushByte(buf[p++], size)
|
|
668
668
|
} else if (type === 2) {
|
|
669
|
-
if (p + size > buf.length) throw fail('compressed block
|
|
669
|
+
if (p + size > buf.length) throw fail('compressed block truncated')
|
|
670
670
|
const block = buf.subarray(p, p + size)
|
|
671
671
|
const lit = readLiterals(block, 0, ctx)
|
|
672
672
|
decodeSequences(block, lit.end, lit.literals, ctx, out)
|
|
673
673
|
p += size
|
|
674
674
|
} else {
|
|
675
|
-
throw fail('
|
|
675
|
+
throw fail('reserved block type')
|
|
676
676
|
}
|
|
677
677
|
}
|
|
678
678
|
|
|
@@ -689,10 +689,10 @@ export function decodeFrames(buf) {
|
|
|
689
689
|
const chunks = []
|
|
690
690
|
let off = 0
|
|
691
691
|
while (off < buf.length) {
|
|
692
|
-
if (off + 4 > buf.length) throw fail('
|
|
693
|
-
if (buf.readUInt32LE(off) !== MAGIC) throw fail('
|
|
692
|
+
if (off + 4 > buf.length) throw fail('trailing bytes shorter than one frame header')
|
|
693
|
+
if (buf.readUInt32LE(off) !== MAGIC) throw fail('bad magic number @' + off)
|
|
694
694
|
const { output, length } = decodeFrame(buf, off)
|
|
695
|
-
if (length <= 0) throw fail('
|
|
695
|
+
if (length <= 0) throw fail('invalid frame length')
|
|
696
696
|
chunks.push(output)
|
|
697
697
|
off += length
|
|
698
698
|
}
|
package/lib/core/zstd.js
CHANGED
|
@@ -47,7 +47,7 @@ function frameLength(buf, off) {
|
|
|
47
47
|
if (fcsFlag === 0 && single) fcs = 1
|
|
48
48
|
q += fcs
|
|
49
49
|
for (;;) {
|
|
50
|
-
if (q + 3 > buf.length) throw new Error('
|
|
50
|
+
if (q + 3 > buf.length) throw new Error('log truncated @' + q)
|
|
51
51
|
const h = buf[q] | (buf[q + 1] << 8) | (buf[q + 2] << 16)
|
|
52
52
|
q += 3
|
|
53
53
|
const last = h & 1
|
package/lib/host/capabilities.js
CHANGED
|
@@ -43,7 +43,7 @@ export function probeCapabilities(ctx, { only } = {}) {
|
|
|
43
43
|
const missing = []
|
|
44
44
|
const need = (block, name, got) => {
|
|
45
45
|
table[block] = got === true
|
|
46
|
-
if (!got) missing.push({ block, why: '
|
|
46
|
+
if (!got) missing.push({ block, why: 'missing ' + name })
|
|
47
47
|
}
|
|
48
48
|
const more = (block, got) => { table[block] = got === true }
|
|
49
49
|
|
|
@@ -92,7 +92,7 @@ export function probeCapabilities(ctx, { only } = {}) {
|
|
|
92
92
|
// 档 5 起「可用」= 原生可用或被内置解码器兜住——所以这条在本插件里恒为 true;
|
|
93
93
|
// 探测函数单独留着,是为了让宿主能力表仍然是一个能查询的真值。
|
|
94
94
|
const zstd = canDecodeZstd()
|
|
95
|
-
need('zstd.decompress', 'zlib.zstdDecompressSync()
|
|
95
|
+
need('zstd.decompress', 'zlib.zstdDecompressSync() or the built-in decoder', zstd)
|
|
96
96
|
|
|
97
97
|
if (Array.isArray(only)) {
|
|
98
98
|
// 只报「这块此刻应该就绪」的条目:apply 刚进来时别把还没起来的服务当成缺失。
|
|
@@ -127,7 +127,7 @@ export function mergeCapability(base, next) {
|
|
|
127
127
|
// 把「还没看到」当成「没有」会误报一整屏。
|
|
128
128
|
base.missing = REQUIRED.filter((k) => Object.prototype.hasOwnProperty.call(base.table, k) && base.table[k] !== true).map((block) => {
|
|
129
129
|
const hit = (next.missing ?? []).find((m) => m.block === block)
|
|
130
|
-
return hit ?? { block, why: '
|
|
130
|
+
return hit ?? { block, why: 'missing ' + block }
|
|
131
131
|
})
|
|
132
132
|
base.ok = base.missing.length === 0
|
|
133
133
|
return base
|
|
@@ -25,13 +25,13 @@ function withTimeout(promise, ms, text) {
|
|
|
25
25
|
* @returns {Promise<{ok:true, deleted:string, memory:string}>}
|
|
26
26
|
*/
|
|
27
27
|
export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit) {
|
|
28
|
-
if (typeof sessionId !== 'string' || sessionId === '') throw new Error('sessionId
|
|
29
|
-
if (deps.compaction === undefined) throw new Error('
|
|
28
|
+
if (typeof sessionId !== 'string' || sessionId === '') throw new Error('sessionId must be a non-empty string')
|
|
29
|
+
if (deps.compaction === undefined) throw new Error('the compaction service is unavailable; cannot delete')
|
|
30
30
|
|
|
31
31
|
const header = (await deps.persistence.list()).map((r) => r.header ?? r).find((h) => h.id === sessionId)
|
|
32
|
-
if (header === undefined) throw new Error('
|
|
32
|
+
if (header === undefined) throw new Error('no such session: ' + sessionId)
|
|
33
33
|
const cwd = header.cwd
|
|
34
|
-
if (typeof cwd !== 'string' || cwd === '') throw new Error('
|
|
34
|
+
if (typeof cwd !== 'string' || cwd === '') throw new Error('this session has no workspace, so no review can be captured')
|
|
35
35
|
|
|
36
36
|
const before = new Set((await listMemories(dshHome, cwd)).entries.map((e) => e.file))
|
|
37
37
|
|
|
@@ -40,7 +40,7 @@ export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit)
|
|
|
40
40
|
if (agent === undefined) {
|
|
41
41
|
handle = await deps.agents.resume({ resumeSessionId: sessionId })
|
|
42
42
|
agent = handle?.agent
|
|
43
|
-
if (agent === undefined) throw new Error('
|
|
43
|
+
if (agent === undefined) throw new Error('failed to resume the session; cannot delete')
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
const controller = new AbortController()
|
|
@@ -51,26 +51,26 @@ export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit)
|
|
|
51
51
|
result = await withTimeout(
|
|
52
52
|
Promise.resolve(deps.compaction.compactNow(agent, controller.signal, 'session-seal-scripts-delete')),
|
|
53
53
|
TIMEOUT_MS,
|
|
54
|
-
'
|
|
54
|
+
'compaction timed out'
|
|
55
55
|
)
|
|
56
56
|
} catch (error) {
|
|
57
57
|
controller.abort()
|
|
58
|
-
throw new Error('
|
|
58
|
+
throw new Error('compaction failed, session not deleted: ' + (error?.message ?? error))
|
|
59
59
|
}
|
|
60
|
-
if (result === null) throw new Error('
|
|
60
|
+
if (result === null) throw new Error('no history to compact, session not deleted')
|
|
61
61
|
|
|
62
62
|
try {
|
|
63
63
|
await waiting
|
|
64
64
|
} catch (error) {
|
|
65
|
-
throw new Error('
|
|
65
|
+
throw new Error('waiting for the review failed, session not deleted: ' + (error?.message ?? error))
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
const after = await listMemories(dshHome, cwd)
|
|
69
69
|
const fresh = after.entries.filter((e) => !before.has(e.file))
|
|
70
|
-
if (fresh.length === 0) throw new Error('
|
|
70
|
+
if (fresh.length === 0) throw new Error('compaction finished but produced no new review, session not deleted')
|
|
71
71
|
|
|
72
72
|
const target = fresh[fresh.length - 1]
|
|
73
73
|
const deleted = await deleteSessionPermanently(deps, sessionId)
|
|
74
|
-
if (!deleted.ok) throw new Error('
|
|
74
|
+
if (!deleted.ok) throw new Error('review was captured (' + target.file + ') but the delete failed: ' + deleted.error.message)
|
|
75
75
|
return { ok: true, deleted: sessionId, memory: target.file }
|
|
76
76
|
}
|
package/lib/host/delete.js
CHANGED
|
@@ -68,19 +68,19 @@ async function closeLive(deps, sessionId) {
|
|
|
68
68
|
const handle = agent === undefined ? undefined : ownedHandlesOf(agent)
|
|
69
69
|
if (handle === undefined || agent.session !== live || typeof agent.cancel !== 'function'
|
|
70
70
|
|| typeof agent.whenIdle !== 'function' || typeof deps.sessions?.flush !== 'function') {
|
|
71
|
-
return { ok: false, code: 'session-busy', message: '
|
|
71
|
+
return { ok: false, code: 'session-busy', message: 'the live session has no public handle available; let its owner stop it first, then delete' }
|
|
72
72
|
}
|
|
73
73
|
try {
|
|
74
74
|
agent.cancel({ kind: 'disposed' })
|
|
75
|
-
await withTimeout(agent.whenIdle(), 'agent
|
|
76
|
-
if (await deps.sessions.flush(live) !== true) throw new Error('
|
|
77
|
-
await withTimeout(handle.dispose(), 'agent
|
|
75
|
+
await withTimeout(agent.whenIdle(), 'the agent did not become idle')
|
|
76
|
+
if (await deps.sessions.flush(live) !== true) throw new Error('no persistence listener confirmed the flush')
|
|
77
|
+
await withTimeout(handle.dispose(), 'the agent release did not finish')
|
|
78
78
|
if (deps.agents.get(sessionId) !== undefined || deps.sessions.get(sessionId) !== undefined) {
|
|
79
|
-
throw new Error('
|
|
79
|
+
throw new Error('live references remain after release')
|
|
80
80
|
}
|
|
81
81
|
return { ok: true }
|
|
82
82
|
} catch (error) {
|
|
83
|
-
return { ok: false, code: 'session-busy', message: '
|
|
83
|
+
return { ok: false, code: 'session-busy', message: 'cannot shut this session down: ' + message(error) + '; the original logs are kept' }
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
|
|
@@ -91,10 +91,10 @@ async function closeLive(deps, sessionId) {
|
|
|
91
91
|
* @returns {Promise<{status:number, ok:boolean, deleted?:string, warnings?:string[], error?:object}>}
|
|
92
92
|
*/
|
|
93
93
|
export async function deleteSessionPermanently(deps, sessionId) {
|
|
94
|
-
if (typeof sessionId !== 'string' || sessionId === '') return failure(400, 'invalid-session-id', 'sessionId
|
|
94
|
+
if (typeof sessionId !== 'string' || sessionId === '') return failure(400, 'invalid-session-id', 'sessionId must be a non-empty string')
|
|
95
95
|
const root = deps.persistence?.config?.root
|
|
96
|
-
if (typeof root !== 'string' || root === '') return failure(503, 'persistence-unavailable', '
|
|
97
|
-
if (deleting.has(sessionId)) return failure(409, 'session-busy', '
|
|
96
|
+
if (typeof root !== 'string' || root === '') return failure(503, 'persistence-unavailable', 'session persistence has no storage root configured')
|
|
97
|
+
if (deleting.has(sessionId)) return failure(409, 'session-busy', 'this session is already being deleted')
|
|
98
98
|
deleting.add(sessionId)
|
|
99
99
|
try {
|
|
100
100
|
let header, workspaces
|
|
@@ -103,31 +103,31 @@ export async function deleteSessionPermanently(deps, sessionId) {
|
|
|
103
103
|
header = all.map((row) => row.header ?? row).find((h) => h.id === sessionId)
|
|
104
104
|
workspaces = deps.workspaceRegistry === undefined ? [] : deps.workspaceRegistry.list()
|
|
105
105
|
if (!Array.isArray(workspaces) || workspaces.some((row) => typeof row.detachSession !== 'function')) {
|
|
106
|
-
throw new Error('workspace
|
|
106
|
+
throw new Error('workspace\'s public detachSession is unavailable')
|
|
107
107
|
}
|
|
108
108
|
} catch (error) {
|
|
109
|
-
return failure(500, 'delete-lookup-failed', '
|
|
109
|
+
return failure(500, 'delete-lookup-failed', 'cannot find the session to delete: ' + message(error))
|
|
110
110
|
}
|
|
111
|
-
if (header === undefined) return failure(404, 'session-not-found', '
|
|
111
|
+
if (header === undefined) return failure(404, 'session-not-found', 'no such session: ' + sessionId)
|
|
112
112
|
|
|
113
113
|
const closed = await closeLive(deps, sessionId)
|
|
114
114
|
if (!closed.ok) return failure(409, closed.code, closed.message)
|
|
115
115
|
|
|
116
116
|
const dir = sessionDirOf(root, header.cwd, sessionId)
|
|
117
|
-
if (!inside(resolve(root), dir)) return failure(500, 'delete-failed', '
|
|
117
|
+
if (!inside(resolve(root), dir)) return failure(500, 'delete-failed', 'the session directory escaped the storage root')
|
|
118
118
|
try {
|
|
119
119
|
await plainDirectory(dir)
|
|
120
120
|
const names = await readdir(dir)
|
|
121
|
-
if (!names.some((n) => PAYLOAD_NAME.test(n))) throw new Error('
|
|
121
|
+
if (!names.some((n) => PAYLOAD_NAME.test(n))) throw new Error('no session log in the directory')
|
|
122
122
|
await rm(dir, { recursive: true, force: true })
|
|
123
123
|
} catch (error) {
|
|
124
|
-
return failure(500, 'delete-failed', '
|
|
124
|
+
return failure(500, 'delete-failed', 'cannot delete the session log: ' + message(error))
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
const warnings = []
|
|
128
128
|
for (const workspace of workspaces) {
|
|
129
129
|
try { await workspace.detachSession(sessionId) }
|
|
130
|
-
catch (error) { warnings.push('
|
|
130
|
+
catch (error) { warnings.push('workspace ' + workspace.id + ' still holds this session: ' + message(error)) }
|
|
131
131
|
}
|
|
132
132
|
return { status: 200, ok: true, deleted: sessionId, ...(warnings.length ? { warnings } : {}) }
|
|
133
133
|
} finally {
|
|
@@ -45,7 +45,7 @@ export function probeHostFormat(sessionDir) {
|
|
|
45
45
|
return {
|
|
46
46
|
ok: false,
|
|
47
47
|
code: 'host-format-changed',
|
|
48
|
-
detail: '
|
|
48
|
+
detail: 'unknown log names appeared: ' + unknown.join(', ')
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -60,7 +60,7 @@ export function probeHostFormat(sessionDir) {
|
|
|
60
60
|
return {
|
|
61
61
|
ok: false,
|
|
62
62
|
code: 'host-format-changed',
|
|
63
|
-
detail: LOG_NAME + '
|
|
63
|
+
detail: LOG_NAME + ' does not start with the zstd magic number'
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
} finally {
|
package/lib/host/http-util.js
CHANGED
|
@@ -74,7 +74,7 @@ export function readBodyBuffer(req, limit = NO_LIMIT) {
|
|
|
74
74
|
chunks.length = 0
|
|
75
75
|
// 已经太大:暂停上游,不再收;交给调用方回 413。
|
|
76
76
|
try { req.pause() } catch { /* 有些 req 没有 pause */ }
|
|
77
|
-
const err = new Error('
|
|
77
|
+
const err = new Error('request body exceeds the limit (' + max + ' bytes)')
|
|
78
78
|
err.code = 'body-too-large'
|
|
79
79
|
no(err)
|
|
80
80
|
return
|
package/lib/host/lifecycle.js
CHANGED
|
@@ -30,13 +30,13 @@ export function installLifecycle(agents, logger) {
|
|
|
30
30
|
if (agents[MARK] === true) return () => {}
|
|
31
31
|
const proto = Object.getPrototypeOf(agents)
|
|
32
32
|
if (proto === null || typeof proto !== 'object') {
|
|
33
|
-
logger?.warn?.('
|
|
33
|
+
logger?.warn?.('host agents have no usable prototype; deleting live sessions is off')
|
|
34
34
|
return () => {}
|
|
35
35
|
}
|
|
36
36
|
// 先把两条都验完再动手:验一条补一条的话,第二条缺了会留下半套补丁。
|
|
37
37
|
for (const method of METHODS) {
|
|
38
38
|
if (typeof proto[method] !== 'function') {
|
|
39
|
-
logger?.warn?.('
|
|
39
|
+
logger?.warn?.('host lacks agents.' + method + '(); deleting live sessions is off')
|
|
40
40
|
return () => {}
|
|
41
41
|
}
|
|
42
42
|
}
|
package/lib/host/register.js
CHANGED
|
@@ -24,11 +24,11 @@ export function makeRoute(webCtx) {
|
|
|
24
24
|
try {
|
|
25
25
|
const rejection = webCtx.connection?.requestRejection?.(req)
|
|
26
26
|
if (rejection !== undefined) {
|
|
27
|
-
respond(res, rejection, { error: { code: 'access-denied', message: '
|
|
27
|
+
respond(res, rejection, { error: { code: 'access-denied', message: 'sign in first' } })
|
|
28
28
|
return
|
|
29
29
|
}
|
|
30
30
|
if (!sameOrigin(req)) {
|
|
31
|
-
respond(res, 403, { error: { code: 'cross-origin', message: '
|
|
31
|
+
respond(res, 403, { error: { code: 'cross-origin', message: 'cross-origin request rejected' } })
|
|
32
32
|
return
|
|
33
33
|
}
|
|
34
34
|
await handler(req, res)
|
|
@@ -39,7 +39,7 @@ export function makeRoute(webCtx) {
|
|
|
39
39
|
}
|
|
40
40
|
// 路径越界是调用方给错了参数,不是服务端坏了:回 400 + 可识别的错误码。
|
|
41
41
|
// 判据在 path-guard.js 里,靠它那句固定开头认人。
|
|
42
|
-
if (
|
|
42
|
+
if (/^only files inside /.test(error?.message ?? '')) {
|
|
43
43
|
respond(res, 400, { error: { code: 'path-outside', message: error.message } })
|
|
44
44
|
return
|
|
45
45
|
}
|
|
@@ -22,9 +22,9 @@ const PREFIX = '/api/session-seal-scripts.'
|
|
|
22
22
|
async function deleteMemory(dshHome, cwd, file) {
|
|
23
23
|
const dir = memoryDir(dshHome, cwd)
|
|
24
24
|
// 判据只留 path-guard 一份(以前这里自带一份,用 startsWith('..') 会把 ..foo.md 误拦)。
|
|
25
|
-
const abs = assertMarkdown(assertInside(dir, file, { label: '
|
|
25
|
+
const abs = assertMarkdown(assertInside(dir, file, { label: 'this workspace\'s review directory' }), file)
|
|
26
26
|
const exists = await stat(abs).then(() => true, () => false)
|
|
27
|
-
if (!exists) throw new Error('
|
|
27
|
+
if (!exists) throw new Error('this review does not exist: ' + file)
|
|
28
28
|
// 顺序不能反:先删正文、再删边车。中间崩溃 → 边车成了孤儿,readAssembly 会自己清掉。
|
|
29
29
|
// 反过来(先删边车)而正文还在,下一次读会按 mtime 把边车补回来——等于没删掉索引。
|
|
30
30
|
await rm(abs, { force: true })
|
|
@@ -51,7 +51,7 @@ export function routesMemories(route, { dshHome }) {
|
|
|
51
51
|
if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
|
|
52
52
|
const body = JSON.parse(await readBody(req) || '{}')
|
|
53
53
|
if (typeof body.cwd !== 'string' || typeof body.file !== 'string') {
|
|
54
|
-
respond(res, 400, { error: { code: 'invalid-body', message: '
|
|
54
|
+
respond(res, 400, { error: { code: 'invalid-body', message: 'cwd and file are required' } }); return
|
|
55
55
|
}
|
|
56
56
|
respond(res, 200, await deleteMemory(dshHome, body.cwd, body.file))
|
|
57
57
|
})
|
|
@@ -60,7 +60,7 @@ export function routesMemories(route, { dshHome }) {
|
|
|
60
60
|
if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
|
|
61
61
|
const body = JSON.parse(await readBody(req) || '{}')
|
|
62
62
|
if (typeof body.cwd !== 'string' || typeof body.file !== 'string') {
|
|
63
|
-
respond(res, 400, { error: { code: 'invalid-body', message: '
|
|
63
|
+
respond(res, 400, { error: { code: 'invalid-body', message: 'cwd and file are required' } }); return
|
|
64
64
|
}
|
|
65
65
|
respond(res, 200, await readMemory(dshHome, body.cwd, body.file))
|
|
66
66
|
})
|
|
@@ -69,18 +69,18 @@ export function routesMemories(route, { dshHome }) {
|
|
|
69
69
|
if (req.method !== 'POST') { respond(res, 405, { error: { code: 'method-not-allowed', message: 'POST' } }); return }
|
|
70
70
|
const body = JSON.parse(await readBody(req) || '{}')
|
|
71
71
|
if (typeof body.cwd !== 'string' || typeof body.file !== 'string') {
|
|
72
|
-
respond(res, 400, { error: { code: 'invalid-body', message: '
|
|
72
|
+
respond(res, 400, { error: { code: 'invalid-body', message: 'cwd and file are required' } }); return
|
|
73
73
|
}
|
|
74
74
|
const hasKeywords = Array.isArray(body.keywords)
|
|
75
75
|
const hasDiary = typeof body.diary === 'string'
|
|
76
76
|
if (!hasKeywords && !hasDiary) {
|
|
77
|
-
respond(res, 400, { error: { code: 'invalid-body', message: 'keywords
|
|
77
|
+
respond(res, 400, { error: { code: 'invalid-body', message: 'pass at least one of keywords or diary' } }); return
|
|
78
78
|
}
|
|
79
79
|
// 写侧必须过闸门:以前这里直接 join + 手写 tmp/rename,能改别的工作区的回顾,
|
|
80
80
|
// 也不受「日记 ≤ 摘要、关键词 < 日记」约束,写不进去时还回 ok:true。
|
|
81
81
|
const dir = memoryDir(dshHome, body.cwd)
|
|
82
82
|
await mkdir(dir, { recursive: true })
|
|
83
|
-
const abs = assertMarkdown(assertInside(dir, body.file, { label: '
|
|
83
|
+
const abs = assertMarkdown(assertInside(dir, body.file, { label: 'this workspace\'s review directory' }), body.file)
|
|
84
84
|
const before = await readFile(abs, 'utf8')
|
|
85
85
|
// 和 session_mem_write 走同一个 applyWrite:两道闸门、只动目标区、改完校验。
|
|
86
86
|
// 它抛什么就回什么——设置页和模型看到的是同一句话。
|