dracosh 0.4.0 → 0.5.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.
@@ -27,7 +27,7 @@ const args = (value) => value.trim().split(/\s+/).slice(1).join(" ");
27
27
  // `commandOnly` is the between-questions variant: it starts at "/" and Esc or erasing the "/" closes it.
28
28
  // `plain` is a text field with no commands at all ("and/or" is just text there).
29
29
  // `onChange` hears every edit; `onRecall` is ↑ on an empty plain field (/add brings back the last card);
30
- // `onPaste` takes a paste of several lines (/add reviews them as cards), anything else is typed in.
30
+ // `onPaste` takes a paste of several lines, as it came (/add reviews them as cards); anything else is typed in.
31
31
  export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, onChange, onRecall, onPaste, commands = COMMANDS, initial = "", commandOnly = false, plain = false }) {
32
32
  const [value, setValue] = useState(initial);
33
33
  const [cursor, setCursor] = useState(initial.length);
@@ -47,8 +47,8 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, onC
47
47
  // pasted text arrives whole (bracketed paste); lines become spaces unless /add takes them as cards
48
48
  const paste = (text) => {
49
49
  const lines = /[\r\n]/.test(text) ? text.split(/\r\n|\r|\n/).filter((line) => line.trim()) : [text];
50
- if (onPaste && lines.length > 1) return onPaste(lines);
51
- const typed = lines.join(" ");
50
+ if (onPaste && lines.length > 1) return onPaste(text);
51
+ const typed = lines.join(" ").replace(/\t/g, " ");
52
52
  edit(value.slice(0, cursor) + typed + value.slice(cursor), cursor + typed.length);
53
53
  };
54
54
  usePaste(paste);
package/src/zip.js ADDED
@@ -0,0 +1,157 @@
1
+ import { open } from "node:fs/promises";
2
+ import { crc32, deflateRawSync, inflateRawSync } from "node:zlib";
3
+
4
+ // A zip file (PKWARE APPNOTE 6.3), the container of an Anki package: each file deflated, names in
5
+ // UTF-8, no zip64 (an export never comes near 4 GB).
6
+ // writeZip([{ name: "media", data: Buffer }], { date }) → Buffer
7
+
8
+ const dosTime = (date) => (date.getHours() << 11) | (date.getMinutes() << 5) | (date.getSeconds() >> 1);
9
+ const dosDate = (date) => ((Math.max(date.getFullYear(), 1980) - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate();
10
+ const UTF8_NAMES = 0x0800;
11
+ const DEFLATE = 8;
12
+ const VERSION = 20; // 2.0: deflate
13
+
14
+ export function writeZip(entries, { date = new Date() } = {}) {
15
+ const locals = [];
16
+ const centrals = [];
17
+ let offset = 0;
18
+ for (const { name, data } of entries) {
19
+ const nameBytes = Buffer.from(name, "utf8");
20
+ const packed = deflateRawSync(data);
21
+ if (packed.length > 0xffffffff || data.length > 0xffffffff || offset > 0xffffffff) throw new Error("Too big for a zip without zip64");
22
+ const crc = crc32(data);
23
+
24
+ const local = Buffer.alloc(30);
25
+ local.writeUInt32LE(0x04034b50, 0);
26
+ local.writeUInt16LE(VERSION, 4);
27
+ local.writeUInt16LE(UTF8_NAMES, 6);
28
+ local.writeUInt16LE(DEFLATE, 8);
29
+ local.writeUInt16LE(dosTime(date), 10);
30
+ local.writeUInt16LE(dosDate(date), 12);
31
+ local.writeUInt32LE(crc, 14);
32
+ local.writeUInt32LE(packed.length, 18);
33
+ local.writeUInt32LE(data.length, 22);
34
+ local.writeUInt16LE(nameBytes.length, 26);
35
+ local.writeUInt16LE(0, 28);
36
+ locals.push(local, nameBytes, packed);
37
+
38
+ const central = Buffer.alloc(46);
39
+ central.writeUInt32LE(0x02014b50, 0);
40
+ central.writeUInt16LE(VERSION, 4);
41
+ central.writeUInt16LE(VERSION, 6);
42
+ central.writeUInt16LE(UTF8_NAMES, 8);
43
+ central.writeUInt16LE(DEFLATE, 10);
44
+ central.writeUInt16LE(dosTime(date), 12);
45
+ central.writeUInt16LE(dosDate(date), 14);
46
+ central.writeUInt32LE(crc, 16);
47
+ central.writeUInt32LE(packed.length, 20);
48
+ central.writeUInt32LE(data.length, 24);
49
+ central.writeUInt16LE(nameBytes.length, 28);
50
+ // extra field, comment, disk number, internal and external attributes: all zero
51
+ central.writeUInt32LE(offset, 42);
52
+ centrals.push(central, nameBytes);
53
+
54
+ offset += local.length + nameBytes.length + packed.length;
55
+ }
56
+ const directory = Buffer.concat(centrals);
57
+ const end = Buffer.alloc(22);
58
+ end.writeUInt32LE(0x06054b50, 0);
59
+ end.writeUInt16LE(entries.length, 8);
60
+ end.writeUInt16LE(entries.length, 10);
61
+ end.writeUInt32LE(directory.length, 12);
62
+ end.writeUInt32LE(offset, 16);
63
+ return Buffer.concat([...locals, directory, end]);
64
+ }
65
+
66
+ const STORED = 0;
67
+ const END = 0x06054b50;
68
+ const END64 = 0x06064b50;
69
+ const END64_LOCATOR = 0x07064b50;
70
+ const CENTRAL = 0x02014b50;
71
+ const LOCAL = 0x04034b50;
72
+ const ZIP64_EXTRA = 0x0001;
73
+ const damaged = (why) => Object.assign(new Error(`Damaged zip file: ${why}`), { code: "DAMAGED_ZIP" });
74
+
75
+ // readZip(file, names, { first }) → Map of name → Buffer, for just the files asked for (an Anki
76
+ // package can hold gigabytes of media next to the collection); `first`: only the first of `names`
77
+ // there is. Stored or deflated files, zip64 too (what Anki writes past 4 GB).
78
+ export async function readZip(file, names, { first = false } = {}) {
79
+ const handle = await open(file, "r");
80
+ try {
81
+ const { size } = await handle.stat();
82
+ const read = async (position, length) => {
83
+ if (position < 0 || length < 0 || position + length > size) throw damaged("a part points outside the file");
84
+ const buffer = Buffer.alloc(length);
85
+ const { bytesRead } = await handle.read(buffer, 0, length, position);
86
+ if (bytesRead < length) throw damaged("the file ends early");
87
+ return buffer;
88
+ };
89
+ // the end record is in the last 22 bytes, or before a comment of up to 64 KB
90
+ const tailStart = Math.max(0, size - 22 - 0xffff);
91
+ const tail = await read(tailStart, size - tailStart);
92
+ let endAt = -1;
93
+ for (let i = tail.length - 22; i >= 0 && endAt < 0; i--) if (tail.readUInt32LE(i) === END) endAt = i;
94
+ if (endAt < 0) throw Object.assign(new Error("Not a zip file"), { code: "NOT_ZIP" });
95
+ let count = tail.readUInt16LE(endAt + 10);
96
+ let directorySize = tail.readUInt32LE(endAt + 12);
97
+ let directoryAt = tail.readUInt32LE(endAt + 16);
98
+ // zip64: the real numbers are in a record of their own, which a locator just before this one finds
99
+ if (count === 0xffff || directorySize === 0xffffffff || directoryAt === 0xffffffff) {
100
+ const locator = endAt - 20;
101
+ if (locator < 0 || tail.readUInt32LE(locator) !== END64_LOCATOR) throw damaged("no zip64 end record");
102
+ const end64 = await read(Number(tail.readBigUInt64LE(locator + 8)), 56);
103
+ if (end64.readUInt32LE(0) !== END64) throw damaged("no zip64 end record");
104
+ count = Number(end64.readBigUInt64LE(32));
105
+ directorySize = Number(end64.readBigUInt64LE(40));
106
+ directoryAt = Number(end64.readBigUInt64LE(48));
107
+ }
108
+ const directory = await read(directoryAt, directorySize);
109
+ const found = new Map();
110
+ for (let at = 0, i = 0; i < count; i++) {
111
+ if (at + 46 > directory.length || directory.readUInt32LE(at) !== CENTRAL) throw damaged("a broken directory");
112
+ const nameLength = directory.readUInt16LE(at + 28);
113
+ const extraLength = directory.readUInt16LE(at + 30);
114
+ const next = at + 46 + nameLength + extraLength + directory.readUInt16LE(at + 32);
115
+ if (next > directory.length) throw damaged("a broken directory");
116
+ const name = directory.toString("utf8", at + 46, at + 46 + nameLength);
117
+ if (names.includes(name)) {
118
+ const entry = { method: directory.readUInt16LE(at + 10), packed: directory.readUInt32LE(at + 20), size: directory.readUInt32LE(at + 24), local: directory.readUInt32LE(at + 42) };
119
+ // zip64 sizes and offset, in that order, only for the ones that didn't fit
120
+ let extra = at + 46 + nameLength;
121
+ const extraEnd = extra + extraLength;
122
+ while (extra + 4 <= extraEnd) {
123
+ const [id, length] = [directory.readUInt16LE(extra), directory.readUInt16LE(extra + 2)];
124
+ if (id === ZIP64_EXTRA) {
125
+ let field = extra + 4;
126
+ for (const key of ["size", "packed", "local"]) {
127
+ if (entry[key] !== 0xffffffff) continue;
128
+ if (field + 8 > extra + 4 + length) throw damaged("a broken zip64 field");
129
+ entry[key] = Number(directory.readBigUInt64LE(field));
130
+ field += 8;
131
+ }
132
+ }
133
+ extra += 4 + length;
134
+ }
135
+ found.set(name, entry);
136
+ }
137
+ at = next;
138
+ }
139
+ const wanted = first ? names.filter((name) => found.has(name)).slice(0, 1) : names.filter((name) => found.has(name));
140
+ const files = new Map();
141
+ for (const name of wanted) {
142
+ const { method, packed: packedSize, size: unpackedSize, local } = found.get(name);
143
+ const header = await read(local, 30);
144
+ if (header.readUInt32LE(0) !== LOCAL) throw damaged(`no header for ${name}`);
145
+ const packed = await read(local + 30 + header.readUInt16LE(26) + header.readUInt16LE(28), packedSize);
146
+ if (method === STORED) files.set(name, packed);
147
+ else if (method === DEFLATE) {
148
+ const data = inflateRawSync(packed);
149
+ if (data.length !== unpackedSize) throw damaged(`${name} unpacks to the wrong size`);
150
+ files.set(name, data);
151
+ } else throw damaged(`unsupported compression in ${name}`);
152
+ }
153
+ return files;
154
+ } finally {
155
+ await handle.close();
156
+ }
157
+ }