dsh-plugin-packer 0.0.0-stage → 2.1.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/zip.js ADDED
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Minimal, dependency-free ZIP support for dsh-plugin-packer.
3
+ *
4
+ * The Harness profile ships no archive library (no jszip / yauzl / tar), so the
5
+ * packer carries its own writer and reader on top of node:zlib. Only the two
6
+ * methods the format needs are produced: STORE (0) and DEFLATE (8). ZIP64 is
7
+ * deliberately unsupported; an over-limit archive fails loudly instead of
8
+ * producing a file other tools cannot read.
9
+ */
10
+ import { readFileSync, statSync } from 'node:fs';
11
+ import { deflateRawSync, inflateRawSync } from 'node:zlib';
12
+
13
+ const SIG_LOCAL = 0x04034b50;
14
+ const SIG_CENTRAL = 0x02014b50;
15
+ const SIG_EOCD = 0x06054b50;
16
+ /** Bit 11: entry names are UTF-8. */
17
+ const FLAG_UTF8 = 0x0800;
18
+ const MAX_ENTRIES = 0xffff;
19
+ const MAX_BYTES = 0xfffffffe;
20
+
21
+ const CRC_TABLE = new Int32Array(256);
22
+ for (let index = 0; index < 256; index += 1) {
23
+ let value = index;
24
+ for (let bit = 0; bit < 8; bit += 1) {
25
+ value = (value & 1) !== 0 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
26
+ }
27
+ CRC_TABLE[index] = value;
28
+ }
29
+
30
+ /**
31
+ * CRC-32 (IEEE 802.3) as ZIP stores it.
32
+ * @param {Uint8Array} buffer - bytes to checksum.
33
+ * @returns {number} unsigned 32-bit checksum.
34
+ */
35
+ export function crc32(buffer) {
36
+ let value = -1;
37
+ for (let index = 0; index < buffer.length; index += 1) {
38
+ value = CRC_TABLE[(value ^ buffer[index]) & 0xff] ^ (value >>> 8);
39
+ }
40
+ return (value ^ -1) >>> 0;
41
+ }
42
+
43
+ function u16(value) {
44
+ const buffer = Buffer.alloc(2);
45
+ buffer.writeUInt16LE(value & 0xffff, 0);
46
+ return buffer;
47
+ }
48
+
49
+ function u32(value) {
50
+ const buffer = Buffer.alloc(4);
51
+ buffer.writeUInt32LE(value >>> 0, 0);
52
+ return buffer;
53
+ }
54
+
55
+ function dosDateTime(date) {
56
+ const year = Math.max(1980, date.getFullYear());
57
+ return {
58
+ time: ((date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2)) & 0xffff,
59
+ date: (((year - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate()) & 0xffff,
60
+ };
61
+ }
62
+
63
+ /**
64
+ * One archive member.
65
+ * @typedef {object} ZipSource
66
+ * @property {string} name - forward-slash archive path.
67
+ * @property {string} [file] - absolute path to read.
68
+ * @property {Buffer} [data] - in-memory bytes (takes precedence).
69
+ * @property {number} [size] - uncompressed size when known.
70
+ * @property {Date} [mtime] - modification time recorded in the entry.
71
+ */
72
+
73
+ /**
74
+ * Encode members into a streaming ZIP.
75
+ *
76
+ * Yields chunks in archive order so the caller can pipe them straight to a
77
+ * response without materializing the whole archive: one member is compressed
78
+ * at a time, and only the central-directory records stay resident.
79
+ *
80
+ * @param {ZipSource[]} sources - members in archive order.
81
+ * @returns {AsyncGenerator<Buffer>} archive chunks.
82
+ */
83
+ export async function* createZipStream(sources) {
84
+ if (sources.length > MAX_ENTRIES) {
85
+ throw new Error('zip: too many entries (' + sources.length + ' > ' + MAX_ENTRIES + ')');
86
+ }
87
+ /** @type {object[]} */
88
+ const records = [];
89
+ let offset = 0;
90
+ for (const source of sources) {
91
+ const raw = source.data ?? readFileSync(source.file);
92
+ const nameBuffer = Buffer.from(source.name, 'utf8');
93
+ const mtime = source.mtime ?? new Date();
94
+ const stamp = dosDateTime(mtime);
95
+ const deflated = raw.length > 64 ? deflateRawSync(raw, { level: 6 }) : raw;
96
+ const useDeflate = deflated.length < raw.length;
97
+ const body = useDeflate ? deflated : raw;
98
+ const method = useDeflate ? 8 : 0;
99
+ const checksum = crc32(raw);
100
+ const local = Buffer.concat([
101
+ u32(SIG_LOCAL), u16(20), u16(FLAG_UTF8), u16(method), u16(stamp.time), u16(stamp.date),
102
+ u32(checksum), u32(body.length), u32(raw.length), u16(nameBuffer.length), u16(0), nameBuffer,
103
+ ]);
104
+ records.push({
105
+ nameBuffer, method, time: stamp.time, date: stamp.date,
106
+ checksum, compressed: body.length, uncompressed: raw.length, offset,
107
+ });
108
+ offset += local.length + body.length;
109
+ if (offset > MAX_BYTES) throw new Error('zip: archive exceeds the 4 GiB format limit');
110
+ yield local;
111
+ yield body;
112
+ }
113
+ const centralStart = offset;
114
+ let centralSize = 0;
115
+ for (const record of records) {
116
+ const header = Buffer.concat([
117
+ u32(SIG_CENTRAL), u16(20), u16(20), u16(FLAG_UTF8), u16(record.method),
118
+ u16(record.time), u16(record.date), u32(record.checksum), u32(record.compressed),
119
+ u32(record.uncompressed), u16(record.nameBuffer.length), u16(0), u16(0), u16(0), u16(0),
120
+ u32(0), u32(record.offset), record.nameBuffer,
121
+ ]);
122
+ centralSize += header.length;
123
+ yield header;
124
+ }
125
+ if (centralStart + centralSize > MAX_BYTES) throw new Error('zip: central directory exceeds the 4 GiB format limit');
126
+ yield Buffer.concat([
127
+ u32(SIG_EOCD), u16(0), u16(0), u16(records.length), u16(records.length),
128
+ u32(centralSize), u32(centralStart), u16(0),
129
+ ]);
130
+ }
131
+
132
+ /**
133
+ * Locate the end-of-central-directory record.
134
+ * @param {Buffer} buffer - complete archive.
135
+ * @returns {number} offset of the EOCD signature.
136
+ */
137
+ function findEocd(buffer) {
138
+ const first = Math.max(0, buffer.length - 0xffff - 22);
139
+ for (let at = buffer.length - 22; at >= first; at -= 1) {
140
+ if (buffer.readUInt32LE(at) === SIG_EOCD) return at;
141
+ }
142
+ throw new Error('zip: not a zip archive (no end-of-central-directory record)');
143
+ }
144
+
145
+ /**
146
+ * Read every member of an archive into memory.
147
+ *
148
+ * Entry names are returned verbatim; the caller is responsible for path
149
+ * safety. Sizes are validated against the declared lengths so a corrupted or
150
+ * hostile archive fails instead of silently truncating.
151
+ *
152
+ * @param {Buffer} buffer - complete archive bytes.
153
+ * @returns {{ name: string, directory: boolean, data: Buffer }[]} members.
154
+ */
155
+ export function readZip(buffer) {
156
+ if (buffer.length < 22) throw new Error('zip: archive is too small');
157
+ const eocd = findEocd(buffer);
158
+ const count = buffer.readUInt16LE(eocd + 10);
159
+ const centralSize = buffer.readUInt32LE(eocd + 12);
160
+ const centralOffset = buffer.readUInt32LE(eocd + 16);
161
+ if (centralOffset + centralSize > buffer.length) throw new Error('zip: central directory out of range');
162
+ if (count > MAX_ENTRIES) throw new Error('zip: too many entries');
163
+ const members = [];
164
+ let at = centralOffset;
165
+ for (let index = 0; index < count; index += 1) {
166
+ if (at + 46 > buffer.length || buffer.readUInt32LE(at) !== SIG_CENTRAL) {
167
+ throw new Error('zip: corrupt central directory at entry ' + index);
168
+ }
169
+ const method = buffer.readUInt16LE(at + 10);
170
+ const compressedSize = buffer.readUInt32LE(at + 20);
171
+ const uncompressedSize = buffer.readUInt32LE(at + 24);
172
+ const nameLength = buffer.readUInt16LE(at + 28);
173
+ const extraLength = buffer.readUInt16LE(at + 30);
174
+ const commentLength = buffer.readUInt16LE(at + 32);
175
+ const localOffset = buffer.readUInt32LE(at + 42);
176
+ const name = buffer.toString('utf8', at + 46, at + 46 + nameLength);
177
+ at += 46 + nameLength + extraLength + commentLength;
178
+ if (uncompressedSize > MAX_BYTES) throw new Error('zip: member "' + name + '" exceeds the size limit');
179
+ if (localOffset + 30 > buffer.length || buffer.readUInt32LE(localOffset) !== SIG_LOCAL) {
180
+ throw new Error('zip: corrupt local header for "' + name + '"');
181
+ }
182
+ const localNameLength = buffer.readUInt16LE(localOffset + 26);
183
+ const localExtraLength = buffer.readUInt16LE(localOffset + 28);
184
+ const dataStart = localOffset + 30 + localNameLength + localExtraLength;
185
+ if (dataStart + compressedSize > buffer.length) throw new Error('zip: member "' + name + '" runs past the end of the archive');
186
+ const payload = buffer.subarray(dataStart, dataStart + compressedSize);
187
+ let data;
188
+ if (method === 0) data = payload;
189
+ else if (method === 8) data = inflateRawSync(payload);
190
+ else throw new Error('zip: unsupported compression method ' + method + ' for "' + name + '"');
191
+ if (data.length !== uncompressedSize) {
192
+ throw new Error('zip: member "' + name + '" size mismatch (' + data.length + ' != ' + uncompressedSize + ')');
193
+ }
194
+ members.push({ name, directory: name.endsWith('/'), data });
195
+ }
196
+ return members;
197
+ }
198
+
199
+ /**
200
+ * Sum a tree's size, used for the catalogue rows.
201
+ * @param {string} file - absolute path.
202
+ * @returns {{ size: number, mtime: Date }} size and modification time.
203
+ */
204
+ export function fileStat(file) {
205
+ const stat = statSync(file);
206
+ return { size: stat.size, mtime: stat.mtime };
207
+ }
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "dsh-plugin-packer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.1.0",
4
+ "description": "DSH 插件打包器:把插件、依赖闭包和运行时环境打成一个可离线导入的 zip,设置页支持按安装/更新时间排序与多选批量打包。",
5
+ "keywords": ["dsh-plugin", "deepseek", "harness", "plugin", "packaging", "export", "import", "offline", "runtime-assets"],
6
+ "type": "module",
7
+ "main": "lib/index.js",
8
+ "exports": {
9
+ ".": "./lib/index.js",
10
+ "./client": "./lib/client.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "files": ["lib", "docs", "cordis.patch.yml", "README.md", "LICENSE"],
14
+ "engines": { "node": ">=20" },
15
+ "scripts": {
16
+ "clean": "node tools/clean.mjs",
17
+ "clean:all": "node tools/clean.mjs --all",
18
+ "test": "node test/host.test.mjs",
19
+ "pack": "node tools/pack-self.mjs",
20
+ "publish:npm": "npm publish"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org"
25
+ },
26
+ "dsh": {
27
+ "bundle": { "patch": "./cordis.patch.yml" },
28
+ "client": { "platform": "web", "inject": [] }
29
+ },
30
+ "license": "MIT"
31
+ }