@samuelchung2010/dshift 0.0.0-stage → 0.3.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/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +116 -0
- package/README.md +116 -3
- package/assets/dshift-readme.gif +0 -0
- package/client.js +18831 -0
- package/cordis.patch.yml +58 -0
- package/docs/architecture.md +203 -0
- package/docs/configuration.md +103 -0
- package/docs/development.md +125 -0
- package/docs/limitations.md +74 -0
- package/docs/math-boxed-qquad.png +0 -0
- package/docs/math-boxed-single.png +0 -0
- package/docs/memory-integration.md +111 -0
- package/docs/perf/baseline-before.json +4872 -0
- package/docs/perf/baseline-before.summary.json +659 -0
- package/docs/perf/fixed-after.json +2927 -0
- package/docs/perf/fixed-after.summary.json +583 -0
- package/docs/perf/long-chat-performance.md +340 -0
- package/docs/perf/merged-after.json +1953 -0
- package/docs/perf/merged-after.summary.json +382 -0
- package/docs/perf/mount-after.json +82 -0
- package/docs/perf/mount-baseline.json +82 -0
- package/docs/user-guide.md +202 -0
- package/icon.svg +13 -0
- package/index.js +16 -0
- package/lib/api.js +336 -0
- package/lib/artifact/approval-bus.js +94 -0
- package/lib/artifact/prompt.js +99 -0
- package/lib/artifact/service.js +602 -0
- package/lib/artifact/workspace.js +260 -0
- package/lib/import/convert.js +343 -0
- package/lib/import/format.js +262 -0
- package/lib/import/index.js +585 -0
- package/lib/import/media.js +101 -0
- package/lib/import/zip.js +296 -0
- package/lib/memory-store.js +629 -0
- package/lib/memory-text.js +162 -0
- package/lib/memory.js +871 -0
- package/lib/model-config.js +280 -0
- package/lib/protocol.js +593 -0
- package/lib/service.js +2102 -0
- package/lib/store.js +1011 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +49 -6
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small, read-only ZIP reader for DeepSeek chat exports.
|
|
3
|
+
*
|
|
4
|
+
* Only Node built-ins are used. The archive is read through a file handle, so a
|
|
5
|
+
* 130 MB export never sits in memory as a whole: the central directory is read
|
|
6
|
+
* once, and an entry is inflated only when a caller names it. Nothing is ever
|
|
7
|
+
* written to a path derived from an entry name, which is what makes a hostile
|
|
8
|
+
* archive harmless here: a name is only ever a lookup key.
|
|
9
|
+
*
|
|
10
|
+
* Safety limits are enforced before any work happens:
|
|
11
|
+
* - the archive size, entry count, and per-entry uncompressed size are capped;
|
|
12
|
+
* - encrypted entries, unsupported compression methods and ZIP64 records are
|
|
13
|
+
* refused with a typed error instead of being guessed at;
|
|
14
|
+
* - inflation is capped by `maxOutputLength`, so a lying header cannot expand
|
|
15
|
+
* into a memory-exhausting stream;
|
|
16
|
+
* - a CRC mismatch is reported as a damaged entry.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { promises as fs } from 'node:fs'
|
|
20
|
+
import { inflateRaw } from 'node:zlib'
|
|
21
|
+
import { promisify } from 'node:util'
|
|
22
|
+
|
|
23
|
+
const inflate = promisify(inflateRaw)
|
|
24
|
+
|
|
25
|
+
const EOCD_SIGNATURE = 0x06054b50
|
|
26
|
+
const CENTRAL_SIGNATURE = 0x02014b50
|
|
27
|
+
const LOCAL_SIGNATURE = 0x04034b50
|
|
28
|
+
const EOCD_MIN_BYTES = 22
|
|
29
|
+
const EOCD_MAX_BYTES = EOCD_MIN_BYTES + 0xffff
|
|
30
|
+
const CENTRAL_MIN_BYTES = 46
|
|
31
|
+
const LOCAL_BYTES = 30
|
|
32
|
+
|
|
33
|
+
/** An archive that cannot be read; `code` names the user-facing reason. */
|
|
34
|
+
export class ZipError extends Error {
|
|
35
|
+
constructor(code, message) {
|
|
36
|
+
super(message)
|
|
37
|
+
this.name = 'ZipError'
|
|
38
|
+
this.code = code
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A ZIP entry the caller may request by name. */
|
|
43
|
+
export function zipEntryInfo(raw) {
|
|
44
|
+
return {
|
|
45
|
+
name: raw.name,
|
|
46
|
+
/** Uncompressed size declared by the central directory. */
|
|
47
|
+
size: raw.size,
|
|
48
|
+
/** Compressed size declared by the central directory. */
|
|
49
|
+
compressedSize: raw.compressedSize,
|
|
50
|
+
compression: raw.compression,
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** CRC-32 table, built once. The polynomial is the standard ZIP one. */
|
|
55
|
+
const CRC_TABLE = (() => {
|
|
56
|
+
const table = new Int32Array(256)
|
|
57
|
+
for (let index = 0; index < 256; index += 1) {
|
|
58
|
+
let value = index
|
|
59
|
+
for (let bit = 0; bit < 8; bit += 1) value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1
|
|
60
|
+
table[index] = value
|
|
61
|
+
}
|
|
62
|
+
return table
|
|
63
|
+
})()
|
|
64
|
+
|
|
65
|
+
function crc32(bytes) {
|
|
66
|
+
let value = -1
|
|
67
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
68
|
+
value = CRC_TABLE[(value ^ bytes[index]) & 0xff] ^ (value >>> 8)
|
|
69
|
+
}
|
|
70
|
+
return (value ^ -1) >>> 0
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function u16(buffer, offset) {
|
|
74
|
+
return buffer.readUInt16LE(offset)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Read exactly `length` bytes at `position`; a short read is retried so a large
|
|
79
|
+
* entry is never parsed from a partially filled buffer.
|
|
80
|
+
*/
|
|
81
|
+
async function readExact(handle, buffer, length, position) {
|
|
82
|
+
let done = 0
|
|
83
|
+
while (done < length) {
|
|
84
|
+
const { bytesRead } = await handle.read(buffer, done, length - done, position + done)
|
|
85
|
+
if (bytesRead <= 0) break
|
|
86
|
+
done += bytesRead
|
|
87
|
+
}
|
|
88
|
+
return done
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function u32(buffer, offset) {
|
|
92
|
+
return buffer.readUInt32LE(offset)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Parse one central-directory record.
|
|
97
|
+
* @returns `{ entry, next }`, or null when the record names a directory.
|
|
98
|
+
*/
|
|
99
|
+
function readCentralEntry(directory, offset) {
|
|
100
|
+
if (offset + CENTRAL_MIN_BYTES > directory.length) {
|
|
101
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the central directory ends mid-record')
|
|
102
|
+
}
|
|
103
|
+
if (u32(directory, offset) !== CENTRAL_SIGNATURE) {
|
|
104
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the central directory holds an unknown record')
|
|
105
|
+
}
|
|
106
|
+
const flags = u16(directory, offset + 8)
|
|
107
|
+
const compression = u16(directory, offset + 10)
|
|
108
|
+
const crc = u32(directory, offset + 16)
|
|
109
|
+
const compressedSize = u32(directory, offset + 20)
|
|
110
|
+
const size = u32(directory, offset + 24)
|
|
111
|
+
const nameBytes = u16(directory, offset + 28)
|
|
112
|
+
const extraBytes = u16(directory, offset + 30)
|
|
113
|
+
const commentBytes = u16(directory, offset + 32)
|
|
114
|
+
const localOffset = u32(directory, offset + 42)
|
|
115
|
+
if (size === 0xffffffff || compressedSize === 0xffffffff || localOffset === 0xffffffff) {
|
|
116
|
+
throw new ZipError('ARCHIVE_UNSUPPORTED', 'ZIP64 archives are not supported')
|
|
117
|
+
}
|
|
118
|
+
const nameStart = offset + CENTRAL_MIN_BYTES
|
|
119
|
+
const nameEnd = nameStart + nameBytes
|
|
120
|
+
if (nameEnd > directory.length) {
|
|
121
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the central directory name is truncated')
|
|
122
|
+
}
|
|
123
|
+
const name = directory.toString('utf8', nameStart, nameEnd)
|
|
124
|
+
const next = nameEnd + extraBytes + commentBytes
|
|
125
|
+
if (next > directory.length) {
|
|
126
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the central directory record is truncated')
|
|
127
|
+
}
|
|
128
|
+
const entry = {
|
|
129
|
+
name,
|
|
130
|
+
flags,
|
|
131
|
+
compression,
|
|
132
|
+
crc,
|
|
133
|
+
compressedSize,
|
|
134
|
+
size,
|
|
135
|
+
localOffset,
|
|
136
|
+
}
|
|
137
|
+
return { entry, next }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Read one ZIP archive from disk.
|
|
142
|
+
*
|
|
143
|
+
* The returned object keeps the handle open until {@link ZipArchive#close}, so a
|
|
144
|
+
* caller that reads many entries pays one directory parse. Entry names are
|
|
145
|
+
* compared exactly and never resolved against the filesystem.
|
|
146
|
+
*/
|
|
147
|
+
export class ZipArchive {
|
|
148
|
+
/**
|
|
149
|
+
* @param handle - open file handle.
|
|
150
|
+
* @param fileSize - archive size in bytes.
|
|
151
|
+
* @param entries - parsed central directory keyed by entry name.
|
|
152
|
+
* @param maxEntryBytes - largest entry this archive may inflate.
|
|
153
|
+
*/
|
|
154
|
+
constructor(handle, fileSize, entries, maxEntryBytes) {
|
|
155
|
+
this.handle = handle
|
|
156
|
+
this.fileSize = fileSize
|
|
157
|
+
this.entries = entries
|
|
158
|
+
this.maxEntryBytes = maxEntryBytes
|
|
159
|
+
this.closed = false
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Open an archive and read its central directory.
|
|
164
|
+
* @param filePath - archive on disk.
|
|
165
|
+
* @param options - limits.
|
|
166
|
+
* @returns the reader.
|
|
167
|
+
* @throws {ZipError} when the file is not a readable ZIP archive or breaks a limit.
|
|
168
|
+
*/
|
|
169
|
+
static async open(filePath, { maxArchiveBytes = 1024 * 1024 * 1024, maxEntryBytes = 64 * 1024 * 1024, maxEntries = 20_000 } = {}) {
|
|
170
|
+
const handle = await fs.open(filePath, 'r')
|
|
171
|
+
try {
|
|
172
|
+
const stat = await handle.stat()
|
|
173
|
+
if (stat.size === 0) throw new ZipError('NOT_ZIP', 'the file is empty')
|
|
174
|
+
if (stat.size > maxArchiveBytes) throw new ZipError('ARCHIVE_TOO_LARGE', 'the archive is larger than the import limit')
|
|
175
|
+
const tailLength = Math.min(stat.size, EOCD_MAX_BYTES)
|
|
176
|
+
const tail = Buffer.alloc(tailLength)
|
|
177
|
+
await readExact(handle, tail, tailLength, stat.size - tailLength)
|
|
178
|
+
let eocd = -1
|
|
179
|
+
for (let offset = tailLength - EOCD_MIN_BYTES; offset >= 0; offset -= 1) {
|
|
180
|
+
if (u32(tail, offset) !== EOCD_SIGNATURE) continue
|
|
181
|
+
eocd = offset
|
|
182
|
+
break
|
|
183
|
+
}
|
|
184
|
+
if (eocd < 0) throw new ZipError('NOT_ZIP', 'the file is not a ZIP archive')
|
|
185
|
+
const diskNumber = u16(tail, eocd + 4)
|
|
186
|
+
const directoryDisk = u16(tail, eocd + 6)
|
|
187
|
+
const entriesOnDisk = u16(tail, eocd + 8)
|
|
188
|
+
const totalEntries = u16(tail, eocd + 10)
|
|
189
|
+
const directorySize = u32(tail, eocd + 12)
|
|
190
|
+
const directoryOffset = u32(tail, eocd + 16)
|
|
191
|
+
if (diskNumber !== 0 || directoryDisk !== 0 || entriesOnDisk !== totalEntries) {
|
|
192
|
+
throw new ZipError('ARCHIVE_UNSUPPORTED', 'multi-volume archives are not supported')
|
|
193
|
+
}
|
|
194
|
+
if (totalEntries === 0xffff || directorySize === 0xffffffff || directoryOffset === 0xffffffff) {
|
|
195
|
+
throw new ZipError('ARCHIVE_UNSUPPORTED', 'ZIP64 archives are not supported')
|
|
196
|
+
}
|
|
197
|
+
if (totalEntries > maxEntries) throw new ZipError('ARCHIVE_UNSUPPORTED', 'the archive holds too many files')
|
|
198
|
+
if (directoryOffset + directorySize > stat.size) {
|
|
199
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the central directory lies outside the archive')
|
|
200
|
+
}
|
|
201
|
+
const directory = Buffer.alloc(directorySize)
|
|
202
|
+
if (directorySize > 0) await readExact(handle, directory, directorySize, directoryOffset)
|
|
203
|
+
const entries = new Map()
|
|
204
|
+
let offset = 0
|
|
205
|
+
for (let index = 0; index < totalEntries; index += 1) {
|
|
206
|
+
const record = readCentralEntry(directory, offset)
|
|
207
|
+
offset = record.next
|
|
208
|
+
const entry = record.entry
|
|
209
|
+
if (entry.name.endsWith('/')) continue
|
|
210
|
+
if (entries.has(entry.name)) {
|
|
211
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the archive lists one file name twice')
|
|
212
|
+
}
|
|
213
|
+
entries.set(entry.name, entry)
|
|
214
|
+
}
|
|
215
|
+
return new ZipArchive(handle, stat.size, entries, maxEntryBytes)
|
|
216
|
+
} catch (error) {
|
|
217
|
+
await handle.close().catch(() => {})
|
|
218
|
+
throw error
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Names of every file entry, in directory order. */
|
|
223
|
+
names() {
|
|
224
|
+
return [...this.entries.keys()]
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Whether the archive holds an entry with this exact name. */
|
|
228
|
+
has(name) {
|
|
229
|
+
return this.entries.has(name)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Declared uncompressed size of one entry, or undefined. */
|
|
233
|
+
sizeOf(name) {
|
|
234
|
+
return this.entries.get(name)?.size
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Inflate one entry.
|
|
239
|
+
* @param name - exact entry name.
|
|
240
|
+
* @param maxBytes - cap for this read; entries above it are refused before inflation.
|
|
241
|
+
* @returns the bytes, or undefined when the archive has no such entry.
|
|
242
|
+
* @throws {ZipError} when the entry is encrypted, unsupported, oversized or damaged.
|
|
243
|
+
*/
|
|
244
|
+
async read(name, maxBytes = this.maxEntryBytes) {
|
|
245
|
+
if (this.closed) throw new ZipError('ARCHIVE_CLOSED', 'the archive is already closed')
|
|
246
|
+
const entry = this.entries.get(name)
|
|
247
|
+
if (entry === undefined) return undefined
|
|
248
|
+
if ((entry.flags & 0x1) === 1) throw new ZipError('ARCHIVE_UNSUPPORTED', 'encrypted entries are not supported')
|
|
249
|
+
if (entry.compression !== 0 && entry.compression !== 8) {
|
|
250
|
+
throw new ZipError('ARCHIVE_UNSUPPORTED', 'unsupported compression method')
|
|
251
|
+
}
|
|
252
|
+
if (entry.size > maxBytes || entry.compressedSize > maxBytes) {
|
|
253
|
+
throw new ZipError('ENTRY_TOO_LARGE', 'the entry is larger than the import limit')
|
|
254
|
+
}
|
|
255
|
+
const header = Buffer.alloc(LOCAL_BYTES)
|
|
256
|
+
await readExact(this.handle, header, LOCAL_BYTES, entry.localOffset)
|
|
257
|
+
if (u32(header, 0) !== LOCAL_SIGNATURE) {
|
|
258
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the entry header is missing')
|
|
259
|
+
}
|
|
260
|
+
const nameBytes = u16(header, 26)
|
|
261
|
+
const extraBytes = u16(header, 28)
|
|
262
|
+
const dataOffset = entry.localOffset + LOCAL_BYTES + nameBytes + extraBytes
|
|
263
|
+
if (dataOffset + entry.compressedSize > this.fileSize) {
|
|
264
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the entry data lies outside the archive')
|
|
265
|
+
}
|
|
266
|
+
const raw = Buffer.alloc(entry.compressedSize)
|
|
267
|
+
if (entry.compressedSize > 0) await readExact(this.handle, raw, entry.compressedSize, dataOffset)
|
|
268
|
+
let bytes
|
|
269
|
+
if (entry.compression === 0) {
|
|
270
|
+
bytes = raw
|
|
271
|
+
} else {
|
|
272
|
+
try {
|
|
273
|
+
bytes = await inflate(raw, { maxOutputLength: Math.max(1, maxBytes) })
|
|
274
|
+
} catch (error) {
|
|
275
|
+
if (error?.code === 'ERR_BUFFER_TOO_LARGE') {
|
|
276
|
+
throw new ZipError('ENTRY_TOO_LARGE', 'the entry expands past the import limit')
|
|
277
|
+
}
|
|
278
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the entry cannot be decompressed')
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (bytes.length !== entry.size) {
|
|
282
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the entry size does not match its directory record')
|
|
283
|
+
}
|
|
284
|
+
if (crc32(bytes) !== entry.crc) {
|
|
285
|
+
throw new ZipError('ARCHIVE_DAMAGED', 'the entry checksum does not match')
|
|
286
|
+
}
|
|
287
|
+
return bytes
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Close the file handle; later reads fail with `ARCHIVE_CLOSED`. */
|
|
291
|
+
async close() {
|
|
292
|
+
if (this.closed) return
|
|
293
|
+
this.closed = true
|
|
294
|
+
await this.handle.close().catch(() => {})
|
|
295
|
+
}
|
|
296
|
+
}
|