Claude / server /zip.js
Cnass's picture
Agent Web 2.0: token tasarrufu, kalici MCP kurulumu, guvenlik, yan menu araclari, 3B avatar ve ses
765fe0a verified
Raw History Blame Contribute Delete
4.85 kB
'use strict';
// A small streaming ZIP writer (stored entries, no compression, no ZIP64):
// folder downloads and voice-dataset exports. Media and archives are already
// compressed and text compresses on the wire anyway, so "store" keeps it
// simple and fast. Limits: < 4 GB in total and < 65535 entries.
const fs = require('fs');
const CRC_TABLE = (() => {
const table = new Uint32Array(256);
for (let n = 0; n < 256; n += 1) {
let c = n;
for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
table[n] = c >>> 0;
}
return table;
})();
function crc32Update(crc, buf) {
let c = crc ^ 0xffffffff;
for (let i = 0; i < buf.length; i += 1) c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
return (c ^ 0xffffffff) >>> 0;
}
function dosTime(date) {
const d = date instanceof Date ? date : new Date();
const time = (d.getHours() << 11) | (d.getMinutes() << 5) | Math.floor(d.getSeconds() / 2);
const day = ((Math.max(1980, d.getFullYear()) - 1980) << 9) | ((d.getMonth() + 1) << 5) | d.getDate();
return { time, day };
}
class ZipWriter {
// out: a writable stream (an HTTP response).
constructor(out) {
this.out = out;
this.offset = 0;
this.entries = [];
}
write(buf) {
this.offset += buf.length;
return new Promise((resolve, reject) => {
if (this.out.destroyed) {
reject(new Error('bağlantı kapandı'));
return;
}
if (this.out.write(buf)) resolve();
else {
const onDrain = () => {
this.out.off('close', onClose);
resolve();
};
const onClose = () => {
this.out.off('drain', onDrain);
reject(new Error('bağlantı kapandı'));
};
this.out.once('drain', onDrain);
this.out.once('close', onClose);
}
});
}
async addEntry(name, source, mtime) {
if (this.entries.length >= 65534) throw new Error('çok fazla dosya');
const nameBuf = Buffer.from(name.replace(/\\/g, '/'), 'utf8');
const { time, day } = dosTime(mtime);
const header = Buffer.alloc(30);
header.writeUInt32LE(0x04034b50, 0);
header.writeUInt16LE(20, 4); // version needed
header.writeUInt16LE(0x0808, 6); // data descriptor + UTF-8 names
header.writeUInt16LE(0, 8); // stored
header.writeUInt16LE(time, 10);
header.writeUInt16LE(day, 12);
header.writeUInt32LE(0, 14);
header.writeUInt32LE(0, 18);
header.writeUInt32LE(0, 22);
header.writeUInt16LE(nameBuf.length, 26);
header.writeUInt16LE(0, 28);
const start = this.offset;
await this.write(header);
await this.write(nameBuf);
let crc = 0;
let size = 0;
if (Buffer.isBuffer(source)) {
crc = crc32Update(0, source);
size = source.length;
if (size) await this.write(source);
} else {
for await (const chunk of fs.createReadStream(source)) {
crc = crc32Update(crc, chunk);
size += chunk.length;
await this.write(chunk);
}
}
if (this.offset > 0xfffffff0) throw new Error('arşiv 4 GB sınırını aşıyor');
const desc = Buffer.alloc(16);
desc.writeUInt32LE(0x08074b50, 0);
desc.writeUInt32LE(crc, 4);
desc.writeUInt32LE(size, 8);
desc.writeUInt32LE(size, 12);
await this.write(desc);
this.entries.push({ nameBuf, crc, size, time, day, offset: start });
}
addFile(name, file, mtime) {
return this.addEntry(name, file, mtime);
}
addBuffer(name, buf, mtime) {
return this.addEntry(name, Buffer.isBuffer(buf) ? buf : Buffer.from(String(buf), 'utf8'), mtime);
}
async finish() {
const start = this.offset;
for (const e of this.entries) {
const h = Buffer.alloc(46);
h.writeUInt32LE(0x02014b50, 0);
h.writeUInt16LE(0x0314, 4); // made by: unix, 2.0
h.writeUInt16LE(20, 6);
h.writeUInt16LE(0x0808, 8);
h.writeUInt16LE(0, 10);
h.writeUInt16LE(e.time, 12);
h.writeUInt16LE(e.day, 14);
h.writeUInt32LE(e.crc, 16);
h.writeUInt32LE(e.size, 20);
h.writeUInt32LE(e.size, 24);
h.writeUInt16LE(e.nameBuf.length, 28);
h.writeUInt16LE(0, 30);
h.writeUInt16LE(0, 32);
h.writeUInt16LE(0, 34);
h.writeUInt16LE(0, 36);
h.writeUInt32LE((0o100644 << 16) >>> 0, 38);
h.writeUInt32LE(e.offset, 42);
await this.write(h);
await this.write(e.nameBuf);
}
const size = this.offset - start;
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(0, 4);
end.writeUInt16LE(0, 6);
end.writeUInt16LE(this.entries.length, 8);
end.writeUInt16LE(this.entries.length, 10);
end.writeUInt32LE(size, 12);
end.writeUInt32LE(start, 16);
end.writeUInt16LE(0, 20);
await this.write(end);
this.out.end();
}
}
module.exports = { ZipWriter, crc32Update };