dsh-sessions-manager 3.5.0 → 3.5.1
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 +2 -4
- package/README.md +2 -4
- package/lib/client.js +18 -4
- package/lib/client.js.map +2 -2
- package/lib/index.js +92 -29
- package/lib/index.js.map +3 -3
- package/package.json +1 -1
- package/src/client/index.jsx +16 -7
- package/src/client/logic.js +8 -0
- package/src/index.js +26 -5
- package/src/zstd-frame.js +68 -34
package/lib/index.js
CHANGED
|
@@ -1,32 +1,67 @@
|
|
|
1
1
|
// src/index.js
|
|
2
|
-
import { mkdir as mkdir4, realpath, rename as rename4, stat, unlink, writeFile as writeFile4 } from "node:fs/promises";
|
|
2
|
+
import { mkdir as mkdir4, readFile as readFile2, realpath, rename as rename4, stat, unlink, writeFile as writeFile4 } from "node:fs/promises";
|
|
3
3
|
import { basename, dirname as dirname2, isAbsolute, join as join4 } from "node:path";
|
|
4
|
-
import { readFileSync as
|
|
4
|
+
import { readFileSync as readFileSync3 } from "node:fs";
|
|
5
5
|
import { homedir as homedir3 } from "node:os";
|
|
6
6
|
|
|
7
7
|
// src/zstd-frame.js
|
|
8
8
|
import zlib from "node:zlib";
|
|
9
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
10
9
|
var ZSTD_MAGIC = 4247762216;
|
|
11
10
|
var CHECKSUM_OPTS = { params: { [zlib.constants.ZSTD_c_checksumFlag]: 1 } };
|
|
12
|
-
function
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
function scanZstdFrames(buf, maxFrames = Number.POSITIVE_INFINITY) {
|
|
12
|
+
const frames = [];
|
|
13
|
+
let offset = 0;
|
|
14
|
+
while (offset < buf.length) {
|
|
15
|
+
const start = offset;
|
|
16
|
+
if (buf.length - offset < 4) return { frames, tornStart: start };
|
|
17
|
+
if (buf.readUInt32LE(offset) !== ZSTD_MAGIC) {
|
|
18
|
+
throw new Error(`\u4F1A\u8BDD\u65E5\u5FD7\u683C\u5F0F\u5F02\u5E38\uFF08\u5B57\u8282 ${offset} \u7684 zstd magic \u65E0\u6548\uFF09`);
|
|
19
|
+
}
|
|
20
|
+
offset += 4;
|
|
21
|
+
if (offset === buf.length) return { frames, tornStart: start };
|
|
22
|
+
const descriptor = buf.readUInt8(offset++);
|
|
23
|
+
if ((descriptor & 24) !== 0) throw new Error(`\u4F1A\u8BDD\u65E5\u5FD7\u683C\u5F0F\u5F02\u5E38\uFF08\u5B57\u8282 ${offset - 1} \u4F7F\u7528\u4FDD\u7559\u5E27\u5934\u4F4D\uFF09`);
|
|
24
|
+
const contentSizeFlag = descriptor >>> 6;
|
|
25
|
+
const singleSegment = (descriptor & 32) !== 0;
|
|
26
|
+
const checksum = (descriptor & 4) !== 0;
|
|
27
|
+
const dictionaryFlag = descriptor & 3;
|
|
28
|
+
const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag;
|
|
29
|
+
const contentSizeBytes = contentSizeFlag === 0 ? singleSegment ? 1 : 0 : 1 << contentSizeFlag;
|
|
30
|
+
const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes;
|
|
31
|
+
if (buf.length - offset < remainingHeaderBytes) return { frames, tornStart: start };
|
|
32
|
+
offset += remainingHeaderBytes;
|
|
33
|
+
for (; ; ) {
|
|
34
|
+
if (buf.length - offset < 3) return { frames, tornStart: start };
|
|
35
|
+
const blockHeader = buf.readUIntLE(offset, 3);
|
|
36
|
+
offset += 3;
|
|
37
|
+
const lastBlock = (blockHeader & 1) !== 0;
|
|
38
|
+
const blockType = blockHeader >>> 1 & 3;
|
|
39
|
+
const blockSize = blockHeader >>> 3;
|
|
40
|
+
if (blockType === 3) throw new Error(`\u4F1A\u8BDD\u65E5\u5FD7\u683C\u5F0F\u5F02\u5E38\uFF08\u5B57\u8282 ${offset - 3} \u4F7F\u7528\u4FDD\u7559\u5757\u7C7B\u578B\uFF09`);
|
|
41
|
+
const payloadBytes = blockType === 1 ? 1 : blockSize;
|
|
42
|
+
if (buf.length - offset < payloadBytes) return { frames, tornStart: start };
|
|
43
|
+
offset += payloadBytes;
|
|
44
|
+
if (lastBlock) break;
|
|
45
|
+
}
|
|
46
|
+
if (checksum) {
|
|
47
|
+
if (buf.length - offset < 4) return { frames, tornStart: start };
|
|
48
|
+
offset += 4;
|
|
49
|
+
}
|
|
50
|
+
frames.push({ start, end: offset });
|
|
51
|
+
if (frames.length === maxFrames) return { frames };
|
|
52
|
+
}
|
|
53
|
+
return { frames };
|
|
54
|
+
}
|
|
55
|
+
function firstFrame(buf) {
|
|
56
|
+
const scan = scanZstdFrames(buf, 1);
|
|
57
|
+
const frame = scan.frames[0];
|
|
58
|
+
if (!frame) throw new Error("\u4F1A\u8BDD\u65E5\u5FD7\u683C\u5F0F\u5F02\u5E38\uFF08\u65E0\u5B8C\u6574 zstd \u5E27\uFF09");
|
|
59
|
+
return frame;
|
|
23
60
|
}
|
|
24
|
-
function
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const end0 = starts.length > 1 ? starts[1] : buf.length;
|
|
29
|
-
const frame0 = buf.subarray(starts[0], end0);
|
|
61
|
+
function rewriteFrame0CwdInMemory(buf, newCwd) {
|
|
62
|
+
const frame = firstFrame(buf);
|
|
63
|
+
const end0 = frame.end;
|
|
64
|
+
const frame0 = buf.subarray(frame.start, end0);
|
|
30
65
|
const text = zlib.zstdDecompressSync(frame0).toString("utf8");
|
|
31
66
|
const nl = text.indexOf("\n");
|
|
32
67
|
const line = nl >= 0 ? text.slice(0, nl) : text;
|
|
@@ -34,11 +69,10 @@ function rewriteFrame0Cwd(filePath, newCwd) {
|
|
|
34
69
|
if (obj.type !== "session") {
|
|
35
70
|
throw new Error(`\u4F1A\u8BDD\u65E5\u5FD7\u683C\u5F0F\u5F02\u5E38\uFF08\u5E270 \u4E0D\u662F session header\uFF0C\u5B9E\u9645 type=${obj.type}\uFF09`);
|
|
36
71
|
}
|
|
37
|
-
if (obj.cwd === newCwd) return;
|
|
38
72
|
obj.cwd = newCwd;
|
|
39
73
|
const newFrame0 = zlib.zstdCompressSync(JSON.stringify(obj) + "\n", CHECKSUM_OPTS);
|
|
40
74
|
const rest = buf.subarray(end0);
|
|
41
|
-
|
|
75
|
+
return Buffer.concat([newFrame0, rest]);
|
|
42
76
|
}
|
|
43
77
|
|
|
44
78
|
// src/markdown.js
|
|
@@ -182,7 +216,7 @@ function renderSessionMarkdown(meta, events, options = {}) {
|
|
|
182
216
|
|
|
183
217
|
// src/star-index.js
|
|
184
218
|
import { mkdir, rename, writeFile } from "node:fs/promises";
|
|
185
|
-
import { readFileSync
|
|
219
|
+
import { readFileSync } from "node:fs";
|
|
186
220
|
import { homedir } from "node:os";
|
|
187
221
|
import { join } from "node:path";
|
|
188
222
|
var STAR_SCHEMA_VERSION = 3;
|
|
@@ -210,7 +244,7 @@ function createStarIndex(options = {}) {
|
|
|
210
244
|
let mutation = Promise.resolve();
|
|
211
245
|
async function read() {
|
|
212
246
|
try {
|
|
213
|
-
return normalizeStarStore(JSON.parse(
|
|
247
|
+
return normalizeStarStore(JSON.parse(readFileSync(indexPath, "utf8")));
|
|
214
248
|
} catch {
|
|
215
249
|
return normalizeStarStore(null);
|
|
216
250
|
}
|
|
@@ -303,7 +337,7 @@ function aggregateStorage(items, options = {}) {
|
|
|
303
337
|
|
|
304
338
|
// src/auto-archive.js
|
|
305
339
|
import { mkdir as mkdir2, rename as rename2, writeFile as writeFile2 } from "node:fs/promises";
|
|
306
|
-
import { readFileSync as
|
|
340
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
307
341
|
import { homedir as homedir2 } from "node:os";
|
|
308
342
|
import { join as join2 } from "node:path";
|
|
309
343
|
var AUTO_ARCHIVE_SCHEMA_VERSION = 4;
|
|
@@ -359,7 +393,7 @@ function createAutoArchiveStore(options = {}) {
|
|
|
359
393
|
let mutation = Promise.resolve();
|
|
360
394
|
async function read() {
|
|
361
395
|
try {
|
|
362
|
-
return normalizeAutoArchiveStore(JSON.parse(
|
|
396
|
+
return normalizeAutoArchiveStore(JSON.parse(readFileSync2(indexPath, "utf8")));
|
|
363
397
|
} catch {
|
|
364
398
|
return normalizeAutoArchiveStore(null);
|
|
365
399
|
}
|
|
@@ -975,7 +1009,7 @@ function apply(ctx) {
|
|
|
975
1009
|
}
|
|
976
1010
|
async function readTrashStore() {
|
|
977
1011
|
try {
|
|
978
|
-
return normalizeTrashStore(JSON.parse(
|
|
1012
|
+
return normalizeTrashStore(JSON.parse(readFileSync3(TRASH_INDEX, "utf8")));
|
|
979
1013
|
} catch (e) {
|
|
980
1014
|
return normalizeTrashStore(null);
|
|
981
1015
|
}
|
|
@@ -1243,12 +1277,41 @@ function apply(ctx) {
|
|
|
1243
1277
|
const newPath = locatePath(newHeaderObj);
|
|
1244
1278
|
if (!oldPath || !newPath || oldPath === newPath) return false;
|
|
1245
1279
|
const backupPath = `${oldPath}.move-backup-${Date.now()}`;
|
|
1280
|
+
const stagedPath = `${newPath}.move-stage-${process.pid}-${Date.now()}`;
|
|
1281
|
+
let destinationInstalled = false;
|
|
1246
1282
|
try {
|
|
1247
1283
|
await mkdir4(dirname2(newPath), { recursive: true });
|
|
1284
|
+
try {
|
|
1285
|
+
await stat(newPath);
|
|
1286
|
+
throw new Error("\u79FB\u52A8\u5931\u8D25\uFF1A\u76EE\u6807\u4F4D\u7F6E\u5DF2\u5B58\u5728\u540C\u540D\u4F1A\u8BDD\u65E5\u5FD7");
|
|
1287
|
+
} catch (e) {
|
|
1288
|
+
if (e && e.code !== "ENOENT") throw e;
|
|
1289
|
+
}
|
|
1248
1290
|
await rename4(oldPath, backupPath);
|
|
1249
|
-
await
|
|
1250
|
-
|
|
1291
|
+
const original = await readFile2(backupPath);
|
|
1292
|
+
const originalFrames = scanZstdFrames(original).frames;
|
|
1293
|
+
if (originalFrames.length === 0) throw new Error("\u79FB\u52A8\u524D\u6821\u9A8C\u5931\u8D25\uFF1A\u4F1A\u8BDD\u65E5\u5FD7\u6CA1\u6709\u5B8C\u6574 zstd \u5E27");
|
|
1294
|
+
const rewritten = rewriteFrame0CwdInMemory(original, canonical);
|
|
1295
|
+
const rewrittenFrames = scanZstdFrames(rewritten).frames;
|
|
1296
|
+
if (rewrittenFrames.length !== originalFrames.length) throw new Error("\u79FB\u52A8\u540E\u6821\u9A8C\u5931\u8D25\uFF1A\u4F1A\u8BDD\u65E5\u5FD7\u5E27\u6570\u53D1\u751F\u53D8\u5316");
|
|
1297
|
+
const originalTail = original.subarray(originalFrames[0].end);
|
|
1298
|
+
const rewrittenTail = rewritten.subarray(rewrittenFrames[0].end);
|
|
1299
|
+
if (!originalTail.equals(rewrittenTail)) throw new Error("\u79FB\u52A8\u540E\u6821\u9A8C\u5931\u8D25\uFF1A\u4F1A\u8BDD\u4E8B\u4EF6\u5185\u5BB9\u53D1\u751F\u53D8\u5316");
|
|
1300
|
+
await writeFile4(stagedPath, rewritten, { mode: 384 });
|
|
1301
|
+
await rename4(stagedPath, newPath);
|
|
1302
|
+
destinationInstalled = true;
|
|
1303
|
+
await unlink(backupPath);
|
|
1251
1304
|
} catch (e) {
|
|
1305
|
+
try {
|
|
1306
|
+
await unlink(stagedPath);
|
|
1307
|
+
} catch (_) {
|
|
1308
|
+
}
|
|
1309
|
+
if (destinationInstalled) {
|
|
1310
|
+
try {
|
|
1311
|
+
await unlink(newPath);
|
|
1312
|
+
} catch (_) {
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1252
1315
|
try {
|
|
1253
1316
|
await rename4(backupPath, oldPath);
|
|
1254
1317
|
} catch (_) {
|