md-pdf-studio 2.2.0 → 2.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.
@@ -0,0 +1,77 @@
1
+ const zlib = require('zlib');
2
+
3
+ /**
4
+ * A ZIP archive writer, enough for the two container formats built on ZIP
5
+ * that this tool exports: DOCX and EPUB.
6
+ *
7
+ * Entries are written in the order given, which EPUB relies on: its
8
+ * `mimetype` entry has to come first and uncompressed. Timestamps are fixed,
9
+ * so the same input gives the same bytes.
10
+ */
11
+
12
+ // 1980-01-01 00:00:00, the earliest date the format can express.
13
+ const DOS_TIME = 0;
14
+ const DOS_DATE = (0 << 9) | (1 << 5) | 1;
15
+
16
+ /**
17
+ * @param {{ name: string, data: Buffer | string, store?: boolean }[]} entries
18
+ * @returns {Buffer}
19
+ */
20
+ function createZip(entries) {
21
+ const localParts = [];
22
+ const centralParts = [];
23
+ let offset = 0;
24
+
25
+ for (const entry of entries) {
26
+ const name = Buffer.from(entry.name, 'utf8');
27
+ const data = Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(entry.data, 'utf8');
28
+ const compressed = entry.store ? data : zlib.deflateRawSync(data, { level: 9 });
29
+ const method = entry.store ? 0 : 8;
30
+ const crc = zlib.crc32(data);
31
+
32
+ const local = Buffer.alloc(30);
33
+ local.writeUInt32LE(0x04034b50, 0);
34
+ local.writeUInt16LE(20, 4); // version needed
35
+ local.writeUInt16LE(0x0800, 6); // names are UTF-8
36
+ local.writeUInt16LE(method, 8);
37
+ local.writeUInt16LE(DOS_TIME, 10);
38
+ local.writeUInt16LE(DOS_DATE, 12);
39
+ local.writeUInt32LE(crc, 14);
40
+ local.writeUInt32LE(compressed.length, 18);
41
+ local.writeUInt32LE(data.length, 22);
42
+ local.writeUInt16LE(name.length, 26);
43
+ local.writeUInt16LE(0, 28);
44
+
45
+ const central = Buffer.alloc(46);
46
+ central.writeUInt32LE(0x02014b50, 0);
47
+ central.writeUInt16LE(20, 4); // version made by
48
+ central.writeUInt16LE(20, 6); // version needed
49
+ central.writeUInt16LE(0x0800, 8);
50
+ central.writeUInt16LE(method, 10);
51
+ central.writeUInt16LE(DOS_TIME, 12);
52
+ central.writeUInt16LE(DOS_DATE, 14);
53
+ central.writeUInt32LE(crc, 16);
54
+ central.writeUInt32LE(compressed.length, 20);
55
+ central.writeUInt32LE(data.length, 24);
56
+ central.writeUInt16LE(name.length, 28);
57
+ central.writeUInt32LE(offset, 42);
58
+
59
+ localParts.push(local, name, compressed);
60
+ centralParts.push(central, name);
61
+ offset += local.length + name.length + compressed.length;
62
+ }
63
+
64
+ const centralSize = centralParts.reduce((size, part) => size + part.length, 0);
65
+ const end = Buffer.alloc(22);
66
+ end.writeUInt32LE(0x06054b50, 0);
67
+ end.writeUInt16LE(entries.length, 8);
68
+ end.writeUInt16LE(entries.length, 10);
69
+ end.writeUInt32LE(centralSize, 12);
70
+ end.writeUInt32LE(offset, 16);
71
+
72
+ return Buffer.concat([...localParts, ...centralParts, end]);
73
+ }
74
+
75
+ module.exports = {
76
+ createZip
77
+ };