@storyteller-platform/epub 0.2.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/dist/index.js ADDED
@@ -0,0 +1,1810 @@
1
+ import {
2
+ ERR_DUPLICATED_NAME,
3
+ Uint8ArrayReader,
4
+ Uint8ArrayWriter,
5
+ ZipReader,
6
+ ZipWriter
7
+ } from "@zip.js/zip.js";
8
+ import { Mutex } from "async-mutex";
9
+ import { XMLBuilder, XMLParser } from "fast-xml-parser";
10
+ import he from "he";
11
+ import memoize from "mem";
12
+ import { lookup } from "mime-types";
13
+ import { nanoid } from "nanoid";
14
+ import { dirname, resolve } from "@storyteller-platform/path";
15
+ class EpubEntry {
16
+ filename;
17
+ entry = null;
18
+ data = null;
19
+ async getData() {
20
+ if (this.data) return this.data;
21
+ const writer = new Uint8ArrayWriter();
22
+ const data = await this.entry.getData(writer);
23
+ this.data = data;
24
+ return this.data;
25
+ }
26
+ setData(data) {
27
+ this.data = data;
28
+ }
29
+ constructor(entry) {
30
+ this.filename = entry.filename;
31
+ if ("data" in entry) {
32
+ this.data = entry.data;
33
+ } else {
34
+ this.entry = entry;
35
+ }
36
+ }
37
+ }
38
+ class Epub {
39
+ constructor(entries, onClose) {
40
+ this.entries = entries;
41
+ this.onClose = onClose;
42
+ this.dataWriter = new Uint8ArrayWriter();
43
+ this.zipWriter = new ZipWriter(this.dataWriter);
44
+ this.readXhtmlItemContents = memoize(
45
+ this.readXhtmlItemContents.bind(this),
46
+ // This isn't unnecessary, the generic here just isn't handling the
47
+ // overloaded method type correctly
48
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
49
+ { cacheKey: ([id, as]) => `${id}:${as ?? "xhtml"}` }
50
+ );
51
+ }
52
+ static xmlParser = new XMLParser({
53
+ allowBooleanAttributes: true,
54
+ preserveOrder: true,
55
+ ignoreAttributes: false,
56
+ parseTagValue: false
57
+ });
58
+ static xhtmlParser = new XMLParser({
59
+ allowBooleanAttributes: true,
60
+ alwaysCreateTextNode: true,
61
+ preserveOrder: true,
62
+ ignoreAttributes: false,
63
+ htmlEntities: true,
64
+ trimValues: false,
65
+ stopNodes: ["*.pre", "*.script"],
66
+ parseTagValue: false,
67
+ updateTag(_tagName, _jPath, attrs) {
68
+ if (attrs && "@_/" in attrs) {
69
+ delete attrs["@_/"];
70
+ }
71
+ return true;
72
+ }
73
+ });
74
+ static xmlBuilder = new XMLBuilder({
75
+ preserveOrder: true,
76
+ format: true,
77
+ ignoreAttributes: false,
78
+ suppressEmptyNode: true
79
+ });
80
+ static xhtmlBuilder = new XMLBuilder({
81
+ preserveOrder: true,
82
+ ignoreAttributes: false,
83
+ stopNodes: ["*.pre", "*.script"],
84
+ suppressEmptyNode: true
85
+ });
86
+ /**
87
+ * Format a duration, provided as a number of seconds, as
88
+ * a SMIL clock value, to be used for Media Overlays.
89
+ *
90
+ * @link https://www.w3.org/TR/epub-33/#sec-duration
91
+ */
92
+ static formatSmilDuration(duration) {
93
+ const hours = Math.floor(duration / 3600);
94
+ const minutes = Math.floor(duration / 60 - hours * 60);
95
+ const secondsAndMillis = duration - minutes * 60 - hours * 3600;
96
+ const [seconds, millis] = secondsAndMillis.toFixed(2).split(".");
97
+ return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.padStart(2, "0")}.${millis ?? "0"}`;
98
+ }
99
+ /**
100
+ * Given an XML structure representing a complete XHTML document,
101
+ * add a `link` element to the `head` of the document.
102
+ *
103
+ * This method modifies the provided XML structure.
104
+ */
105
+ static addLinkToXhtmlHead(xml, link) {
106
+ const html = Epub.findXmlChildByName("html", xml);
107
+ if (!html) throw new Error("Invalid XHTML document: no html element");
108
+ const head = Epub.findXmlChildByName("head", html.html);
109
+ if (!head) throw new Error("Invalid XHTML document: no head element");
110
+ head["head"].push({
111
+ link: [],
112
+ ":@": {
113
+ "@_rel": link.rel,
114
+ "@_href": link.href,
115
+ "@_type": link.type
116
+ }
117
+ });
118
+ }
119
+ /**
120
+ * Given an XML structure representing a complete XHTML document,
121
+ * return the sub-structure representing the children of the
122
+ * document's body element.
123
+ */
124
+ static getXhtmlBody(xml) {
125
+ const html = Epub.findXmlChildByName("html", xml);
126
+ if (!html) throw new Error("Invalid XHTML document: no html element");
127
+ const body = Epub.findXmlChildByName("body", html["html"]);
128
+ if (!body) throw new Error("Invalid XHTML document: No body element");
129
+ return body["body"];
130
+ }
131
+ static createXmlElement(name, properties, children = []) {
132
+ return {
133
+ ":@": Object.fromEntries(
134
+ Object.entries(properties).map(([prop, value]) => [`@_${prop}`, value])
135
+ ),
136
+ [name]: children
137
+ };
138
+ }
139
+ static createXmlTextNode(text) {
140
+ return { ["#text"]: text };
141
+ }
142
+ /**
143
+ * Given an XML structure representing a complete XHTML document,
144
+ * return a string representing the concatenation of all text nodes
145
+ * in the document.
146
+ */
147
+ static getXhtmlTextContent(xml) {
148
+ let text = "";
149
+ for (const child of xml) {
150
+ if (Epub.isXmlTextNode(child)) {
151
+ text += child["#text"];
152
+ continue;
153
+ }
154
+ const children = Epub.getXmlChildren(child);
155
+ text += Epub.getXhtmlTextContent(children);
156
+ }
157
+ return text;
158
+ }
159
+ /**
160
+ * Given an XMLElement, return its tag name.
161
+ */
162
+ static getXmlElementName(element) {
163
+ const keys = Object.keys(element);
164
+ const elementName = keys.find((key) => key !== ":@" && key !== "#text");
165
+ if (!elementName)
166
+ throw new Error(
167
+ `Invalid XML Element: missing tag name
168
+ ${JSON.stringify(element, null, 2)}`
169
+ );
170
+ return elementName;
171
+ }
172
+ /**
173
+ * Given an XMLElement, return a list of its children
174
+ */
175
+ static getXmlChildren(element) {
176
+ const elementName = Epub.getXmlElementName(element);
177
+ return element[elementName];
178
+ }
179
+ static replaceXmlChildren(element, children) {
180
+ const elementName = Epub.getXmlElementName(element);
181
+ element[elementName] = children;
182
+ }
183
+ /**
184
+ * Given an XML structure, find the first child matching
185
+ * the provided name and optional filter.
186
+ */
187
+ static findXmlChildByName(name, xml, filter) {
188
+ const element = xml.find((e) => name in e && (filter ? filter(e) : true));
189
+ return element;
190
+ }
191
+ /**
192
+ * Given an XMLNode, determine whether it represents
193
+ * a text node or an XML element.
194
+ */
195
+ static isXmlTextNode(node) {
196
+ return "#text" in node;
197
+ }
198
+ zipWriter;
199
+ dataWriter;
200
+ rootfile = null;
201
+ manifest = null;
202
+ spine = null;
203
+ packageMutex = new Mutex();
204
+ /**
205
+ * Close the Epub. Must be called before the Epub goes out
206
+ * of scope/is garbage collected.
207
+ */
208
+ async close() {
209
+ var _a;
210
+ await ((_a = this.onClose) == null ? void 0 : _a.call(this));
211
+ await this.zipWriter.close();
212
+ }
213
+ /**
214
+ * Construct an Epub instance, optionally beginning
215
+ * with the provided metadata.
216
+ *
217
+ * @param dublinCore Core metadata terms
218
+ * @param additionalMetadata An array of additional metadata entries
219
+ */
220
+ static async create({
221
+ title,
222
+ language,
223
+ identifier,
224
+ date,
225
+ subjects,
226
+ type,
227
+ creators,
228
+ contributors
229
+ }, additionalMetadata = []) {
230
+ const entries = [];
231
+ const encoder = new TextEncoder();
232
+ const container = encoder.encode(`<?xml version="1.0"?>
233
+ <container>
234
+ <rootfiles>
235
+ <rootfile media-type="application/oebps-package+xml" full-path="OEBPS/content.opf"/>
236
+ </rootfiles>
237
+ </container>
238
+ `);
239
+ entries.push(
240
+ new EpubEntry({ filename: "META-INF/container.xml", data: container })
241
+ );
242
+ const packageDocument = encoder.encode(`<?xml version="1.0"?>
243
+ <package unique-identifier="pub-id" dir="${language.textInfo.direction}" xml:lang=${language.toString()} version="3.0">
244
+ <metadata>
245
+ </metadata>
246
+ <manifest>
247
+ </manifest>
248
+ <spine>
249
+ </spine>
250
+ </package>
251
+ `);
252
+ entries.push(
253
+ new EpubEntry({ filename: "OEBPS/content.opf", data: packageDocument })
254
+ );
255
+ const epub = new this(entries);
256
+ const metadata = [
257
+ {
258
+ id: "pub-id",
259
+ type: "dc:identifier",
260
+ properties: {},
261
+ value: identifier
262
+ },
263
+ ...additionalMetadata
264
+ ];
265
+ await Promise.all(metadata.map((entry) => epub.addMetadata(entry)));
266
+ await epub.setTitle(title);
267
+ await epub.setLanguage(language);
268
+ if (date) await epub.setPublicationDate(date);
269
+ if (type) await epub.setType(type);
270
+ if (subjects) {
271
+ await Promise.all(subjects.map((subject) => epub.addSubject(subject)));
272
+ }
273
+ if (creators) {
274
+ await Promise.all(creators.map((creator) => epub.addCreator(creator)));
275
+ }
276
+ if (contributors) {
277
+ await Promise.all(
278
+ contributors.map((contributor) => epub.addCreator(contributor))
279
+ );
280
+ }
281
+ return epub;
282
+ }
283
+ /**
284
+ * Construct an Epub instance by reading an existing EPUB
285
+ * publication.
286
+ *
287
+ * @param pathOrData Must be either a string representing the
288
+ * path to an EPUB file on disk, or a Uint8Array representing
289
+ * the data of the EPUB publication.
290
+ */
291
+ static async from(pathOrData) {
292
+ if (typeof pathOrData === "string") {
293
+ throw new Error("Import from /node to construct from a file");
294
+ }
295
+ const fileData = pathOrData;
296
+ const dataReader = new Uint8ArrayReader(fileData);
297
+ const zipReader = new ZipReader(dataReader);
298
+ const zipEntries = await zipReader.getEntries();
299
+ const epubEntries = zipEntries.map((entry) => new EpubEntry(entry));
300
+ const epub = new this(epubEntries, () => zipReader.close());
301
+ return epub;
302
+ }
303
+ getEntry(path) {
304
+ return this.entries.find((entry) => entry.filename === path);
305
+ }
306
+ async removeEntry(href) {
307
+ const rootfile = await this.getRootfile();
308
+ const filename = this.resolveHref(rootfile, href);
309
+ const index = this.entries.findIndex((entry) => entry.filename === filename);
310
+ if (index === -1) return;
311
+ this.entries.splice(index, 1);
312
+ }
313
+ async getFileData(path, encoding) {
314
+ const containerEntry = this.getEntry(path);
315
+ if (!containerEntry)
316
+ throw new Error(
317
+ `Could not get file data for entry ${path}: entry not found`
318
+ );
319
+ const containerContents = await containerEntry.getData();
320
+ return encoding === "utf-8" ? new TextDecoder("utf-8").decode(containerContents) : containerContents;
321
+ }
322
+ async getRootfile() {
323
+ var _a;
324
+ if (this.rootfile !== null) return this.rootfile;
325
+ const containerString = await this.getFileData(
326
+ "META-INF/container.xml",
327
+ "utf-8"
328
+ );
329
+ if (!containerString)
330
+ throw new Error("Failed to parse EPUB: Missing META-INF/container.xml");
331
+ const containerDocument = Epub.xmlParser.parse(containerString);
332
+ const container = Epub.findXmlChildByName("container", containerDocument);
333
+ if (!container)
334
+ throw new Error(
335
+ "Failed to parse EPUB container.xml: Found no container element"
336
+ );
337
+ const rootfiles = Epub.findXmlChildByName(
338
+ "rootfiles",
339
+ Epub.getXmlChildren(container)
340
+ );
341
+ if (!rootfiles)
342
+ throw new Error(
343
+ "Failed to parse EPUB container.xml: Found no rootfiles element"
344
+ );
345
+ const rootfile = Epub.findXmlChildByName(
346
+ "rootfile",
347
+ Epub.getXmlChildren(rootfiles),
348
+ (node) => {
349
+ var _a2;
350
+ return !Epub.isXmlTextNode(node) && ((_a2 = node[":@"]) == null ? void 0 : _a2["@_media-type"]) === "application/oebps-package+xml";
351
+ }
352
+ );
353
+ if (!((_a = rootfile == null ? void 0 : rootfile[":@"]) == null ? void 0 : _a["@_full-path"]))
354
+ throw new Error(
355
+ "Failed to parse EPUB container.xml: Found no rootfile element"
356
+ );
357
+ this.rootfile = rootfile[":@"]["@_full-path"];
358
+ return this.rootfile;
359
+ }
360
+ migratePackageDocument(packageDocument) {
361
+ for (const element of packageDocument) {
362
+ if (Epub.isXmlTextNode(element)) continue;
363
+ const elementName = Epub.getXmlElementName(element);
364
+ if (elementName.startsWith("opf:")) {
365
+ element[elementName.replace("opf:", "")] = Epub.getXmlChildren(element);
366
+ delete element[elementName];
367
+ this.migratePackageDocument(Epub.getXmlChildren(element));
368
+ }
369
+ }
370
+ }
371
+ async getPackageDocument() {
372
+ const rootfile = await this.getRootfile();
373
+ const packageDocumentString = await this.getFileData(rootfile, "utf-8");
374
+ if (!packageDocumentString)
375
+ throw new Error(
376
+ `Failed to parse EPUB: could not find package document at ${rootfile}`
377
+ );
378
+ const packageDocument = Epub.xmlParser.parse(
379
+ packageDocumentString
380
+ );
381
+ return packageDocument;
382
+ }
383
+ async getPackageElement() {
384
+ const packageDocument = await this.getPackageDocument();
385
+ const packageElement = Epub.findXmlChildByName("package", packageDocument) ?? Epub.findXmlChildByName("opf:package", packageDocument);
386
+ if (!packageElement) {
387
+ throw new Error(
388
+ "Failed to parse EPUB: Found no package element in package document"
389
+ );
390
+ }
391
+ this.migratePackageDocument(packageDocument);
392
+ return packageElement;
393
+ }
394
+ /**
395
+ * Safely modify the package document, without race conditions.
396
+ *
397
+ * Since the reading the package document is an async process,
398
+ * multiple simultaneously dispatched function calls that all
399
+ * attempt to modify it can clobber each other's changes. This
400
+ * method uses a mutex to ensure that each update runs exclusively.
401
+ *
402
+ * @param producer The function to update the package document. If
403
+ * it returns a new package document, that will be persisted, otherwise
404
+ * it will be assumed that the package document was modified in place.
405
+ */
406
+ async withPackage(producer) {
407
+ await this.packageMutex.runExclusive(async () => {
408
+ const packageDocument = await this.getPackageDocument();
409
+ const packageElement = Epub.findXmlChildByName("package", packageDocument) ?? Epub.findXmlChildByName("opf:package", packageDocument);
410
+ if (!packageElement) {
411
+ throw new Error(
412
+ "Failed to parse EPUB: Found no package element in package document"
413
+ );
414
+ }
415
+ const produced = await producer(packageElement);
416
+ const updatedPackageDocument = await Epub.xmlBuilder.build(
417
+ produced ?? packageDocument
418
+ );
419
+ const rootfile = await this.getRootfile();
420
+ this.writeEntryContents(rootfile, updatedPackageDocument, "utf-8");
421
+ });
422
+ }
423
+ /**
424
+ * Retrieve the manifest for the Epub.
425
+ *
426
+ * This is represented as a map from each manifest items'
427
+ * id to the rest of its properties.
428
+ *
429
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
430
+ */
431
+ async getManifest() {
432
+ if (this.manifest !== null) return this.manifest;
433
+ const packageElement = await this.getPackageElement();
434
+ const manifest = Epub.findXmlChildByName(
435
+ "manifest",
436
+ Epub.getXmlChildren(packageElement)
437
+ );
438
+ if (!manifest)
439
+ throw new Error(
440
+ "Failed to parse EPUB: Found no manifest element in package document"
441
+ );
442
+ this.manifest = Epub.getXmlChildren(manifest).reduce((acc, item) => {
443
+ var _a, _b;
444
+ if (Epub.isXmlTextNode(item)) return acc;
445
+ if (!((_a = item[":@"]) == null ? void 0 : _a["@_id"]) || !item[":@"]["@_href"]) {
446
+ return acc;
447
+ }
448
+ return {
449
+ ...acc,
450
+ [item[":@"]["@_id"]]: {
451
+ id: item[":@"]["@_id"],
452
+ href: item[":@"]["@_href"],
453
+ mediaType: item[":@"]["@_media-type"],
454
+ mediaOverlay: item[":@"]["@_media-overlay"],
455
+ fallback: item[":@"]["@_fallback"],
456
+ properties: (_b = item[":@"]["@_properties"]) == null ? void 0 : _b.split(" ")
457
+ }
458
+ };
459
+ }, {});
460
+ return this.manifest;
461
+ }
462
+ /**
463
+ * Returns the first index in the metadata element's children array
464
+ * that matches the provided predicate.
465
+ *
466
+ * Note: This may technically be different than the index in the
467
+ * getMetadata() array, as it includes non-metadata nodes, like
468
+ * text nodes. These are technically not allowed, but may exist,
469
+ * nonetheless. As consumers only ever see the getMetadata()
470
+ * array, this method is only meant to be used internally.
471
+ */
472
+ findMetadataIndex(packageElement, predicate) {
473
+ const metadataElement = Epub.findXmlChildByName(
474
+ "metadata",
475
+ Epub.getXmlChildren(packageElement)
476
+ );
477
+ if (!metadataElement)
478
+ throw new Error(
479
+ "Failed to parse EPUB: Found no metadata element in package document"
480
+ );
481
+ return metadataElement.metadata.findIndex((node) => {
482
+ const item = Epub.parseMetadataItem(node);
483
+ if (!item) return false;
484
+ return predicate(item);
485
+ });
486
+ }
487
+ /**
488
+ * Returns the item in the metadata element's children array
489
+ * that matches the provided predicate.
490
+ */
491
+ async findMetadataItem(predicate) {
492
+ const [first] = await this.findAllMetadataItems(predicate);
493
+ return first ?? null;
494
+ }
495
+ /**
496
+ * Returns the item in the metadata element's children array
497
+ * that matches the provided predicate.
498
+ */
499
+ async findAllMetadataItems(predicate) {
500
+ const packageElement = await this.getPackageElement();
501
+ const metadataElement = Epub.findXmlChildByName(
502
+ "metadata",
503
+ Epub.getXmlChildren(packageElement)
504
+ );
505
+ if (!metadataElement)
506
+ throw new Error(
507
+ "Failed to parse EPUB: Found no metadata element in package document"
508
+ );
509
+ const elements = metadataElement.metadata.filter((node) => {
510
+ const item = Epub.parseMetadataItem(node);
511
+ if (!item) return false;
512
+ return predicate(item);
513
+ });
514
+ return elements.map((element) => Epub.parseMetadataItem(element)).filter((item) => !!item);
515
+ }
516
+ static parseMetadataItem(node) {
517
+ if (Epub.isXmlTextNode(node)) return null;
518
+ const elementName = Epub.getXmlElementName(node);
519
+ const textNode = Epub.getXmlChildren(node)[0];
520
+ const value = !textNode || !Epub.isXmlTextNode(textNode) ? void 0 : textNode["#text"].replaceAll(/\s+/g, " ");
521
+ const attributes = node[":@"] ?? {};
522
+ const { id, ...properties } = Object.fromEntries(
523
+ Object.entries(attributes).map(([attrName, value2]) => [
524
+ attrName.slice(2),
525
+ value2
526
+ ])
527
+ );
528
+ return {
529
+ id,
530
+ type: elementName,
531
+ properties,
532
+ value
533
+ };
534
+ }
535
+ /**
536
+ * Retrieve the metadata entries for the Epub.
537
+ *
538
+ * This is represented as an array of metadata entries,
539
+ * in the order that they're presented in the Epub package document.
540
+ *
541
+ * For more useful semantic representations of metadata, use
542
+ * specific methods such as `getTitle()` and `getAuthors()`.
543
+ *
544
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
545
+ */
546
+ async getMetadata() {
547
+ const packageElement = await this.getPackageElement();
548
+ const metadataElement = Epub.findXmlChildByName(
549
+ "metadata",
550
+ Epub.getXmlChildren(packageElement)
551
+ );
552
+ if (!metadataElement)
553
+ throw new Error(
554
+ "Failed to parse EPUB: Found no metadata element in package document"
555
+ );
556
+ const metadata = metadataElement.metadata.map((node) => Epub.parseMetadataItem(node)).filter((node) => !!node);
557
+ return metadata;
558
+ }
559
+ /**
560
+ * Even "EPUB 3" publications sometimes still only use the
561
+ * EPUB 2 specification for identifying the cover image.
562
+ * This is a private method that is used as a fallback if
563
+ * we fail to find the cover image according to the EPUB 3
564
+ * spec.
565
+ */
566
+ async getEpub2CoverImage() {
567
+ var _a;
568
+ const packageElement = await this.getPackageElement();
569
+ const metadataElement = Epub.findXmlChildByName(
570
+ "metadata",
571
+ Epub.getXmlChildren(packageElement)
572
+ );
573
+ if (!metadataElement)
574
+ throw new Error(
575
+ "Failed to parse EPUB: Found no metadata element in package document"
576
+ );
577
+ const coverImageElement = Epub.getXmlChildren(metadataElement).find(
578
+ (node) => {
579
+ var _a2;
580
+ return !Epub.isXmlTextNode(node) && ((_a2 = node[":@"]) == null ? void 0 : _a2["@_name"]) === "cover";
581
+ }
582
+ );
583
+ const manifestItemId = (_a = coverImageElement == null ? void 0 : coverImageElement[":@"]) == null ? void 0 : _a["@_content"];
584
+ if (!manifestItemId) return null;
585
+ const manifest = await this.getManifest();
586
+ return Object.values(manifest).find((item) => item.id === manifestItemId) ?? null;
587
+ }
588
+ /**
589
+ * Retrieve the cover image manifest item.
590
+ *
591
+ * This does not return the actual image data. To
592
+ * retrieve the image data, pass this item's id to
593
+ * epub.readItemContents, or use epub.getCoverImage()
594
+ * instead.
595
+ *
596
+ * @link https://www.w3.org/TR/epub-33/#sec-cover-image
597
+ */
598
+ async getCoverImageItem() {
599
+ const manifest = await this.getManifest();
600
+ const coverImage = Object.values(manifest).find(
601
+ (item) => {
602
+ var _a;
603
+ return (_a = item.properties) == null ? void 0 : _a.includes("cover-image");
604
+ }
605
+ );
606
+ if (coverImage) return coverImage;
607
+ return this.getEpub2CoverImage();
608
+ }
609
+ /**
610
+ * Retrieve the cover image data as a byte array.
611
+ *
612
+ * This does not include, for example, the cover image's
613
+ * filename or mime type. To retrieve the image manifest
614
+ * item, use epub.getCoverImageItem().
615
+ *
616
+ * @link https://www.w3.org/TR/epub-33/#sec-cover-image
617
+ */
618
+ async getCoverImage() {
619
+ const coverImageItem = await this.getCoverImageItem();
620
+ if (!coverImageItem) return coverImageItem;
621
+ return this.readItemContents(coverImageItem.id);
622
+ }
623
+ /**
624
+ * Set the cover image for the EPUB.
625
+ *
626
+ * Adds a manifest item with the `cover-image` property, per
627
+ * the EPUB 3 spec, and then writes the provided image data to
628
+ * the provided href within the publication.
629
+ */
630
+ async setCoverImage(href, data) {
631
+ const coverImageItem = await this.getCoverImageItem();
632
+ if (coverImageItem) {
633
+ await this.removeManifestItem(coverImageItem.id);
634
+ }
635
+ const mediaType = lookup(href);
636
+ if (!mediaType)
637
+ throw new Error(`Invalid file extension for cover image: ${href}`);
638
+ await this.addManifestItem(
639
+ { id: "cover-image", href, mediaType, properties: ["cover-image"] },
640
+ data
641
+ );
642
+ }
643
+ /**
644
+ * Retrieve the publication date from the dc:date element
645
+ * in the EPUB metadata as a Date object.
646
+ *
647
+ * If there is no dc:date element, returns null.
648
+ *
649
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
650
+ */
651
+ async getPublicationDate() {
652
+ const metadata = await this.getMetadata();
653
+ const entry = metadata.find(({ type }) => type === "dc:date");
654
+ if (!(entry == null ? void 0 : entry.value)) return null;
655
+ return new Date(entry.value);
656
+ }
657
+ /**
658
+ * Set the dc:date metadata element with the provided date.
659
+ *
660
+ * Updates the existing dc:date element if one exists.
661
+ * Otherwise creates a new element
662
+ *
663
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcdate
664
+ */
665
+ async setPublicationDate(date) {
666
+ await this.replaceMetadata(({ type }) => type === "dc:date", {
667
+ type: "dc:date",
668
+ properties: {},
669
+ value: date.toISOString()
670
+ });
671
+ }
672
+ /**
673
+ * Set the dc:type metadata element.
674
+ *
675
+ * Updates the existing dc:type element if one exists.
676
+ * Otherwise creates a new element.
677
+ *
678
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
679
+ */
680
+ async setType(type) {
681
+ await this.replaceMetadata(({ type: type2 }) => type2 === "dc:type", {
682
+ type: "dc:type",
683
+ properties: {},
684
+ value: type
685
+ });
686
+ }
687
+ /**
688
+ * Retrieve the publication type from the dc:type element
689
+ * in the EPUB metadata.
690
+ *
691
+ * If there is no dc:type element, returns null.
692
+ *
693
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctype
694
+ */
695
+ async getType() {
696
+ const metadata = await this.getMetadata();
697
+ return metadata.find(({ type }) => type === "dc:type") ?? null;
698
+ }
699
+ /**
700
+ * Add a subject to the EPUB metadata.
701
+ *
702
+ * @param subject May be a string representing just a schema-less
703
+ * subject name, or a DcSubject object
704
+ *
705
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
706
+ */
707
+ async addSubject(subject) {
708
+ const subjectEntry = typeof subject === "string" ? {
709
+ value: subject
710
+ } : subject;
711
+ const subjectId = nanoid();
712
+ await this.addMetadata({
713
+ id: subjectId,
714
+ type: "dc:subject",
715
+ properties: {},
716
+ value: subjectEntry.value
717
+ });
718
+ if ("authority" in subjectEntry) {
719
+ await this.addMetadata({
720
+ type: "meta",
721
+ properties: { refines: `#${subjectId}`, property: "authority" },
722
+ value: subjectEntry.authority
723
+ });
724
+ await this.addMetadata({
725
+ type: "meta",
726
+ properties: { refines: `#${subjectId}`, property: "term" },
727
+ value: subjectEntry.term
728
+ });
729
+ }
730
+ }
731
+ /**
732
+ * Remove a subject from the EPUB metadata.
733
+ *
734
+ * Removes the subject at the provided index. This index
735
+ * refers to the array returned by `epub.getSubjects()`.
736
+ *
737
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
738
+ */
739
+ async removeSubject(index) {
740
+ await this.withPackage((packageElement) => {
741
+ const metadata = Epub.findXmlChildByName(
742
+ "metadata",
743
+ Epub.getXmlChildren(packageElement)
744
+ );
745
+ if (!metadata)
746
+ throw new Error(
747
+ "Failed to parse EPUB: found no metadata element in package document"
748
+ );
749
+ let subjectCount = null;
750
+ let metadataIndex = null;
751
+ for (const meta of Epub.getXmlChildren(metadata)) {
752
+ if (subjectCount === index) break;
753
+ metadataIndex = metadataIndex === null ? 0 : metadataIndex + 1;
754
+ if (Epub.isXmlTextNode(meta)) continue;
755
+ if (Epub.getXmlElementName(meta) !== "dc:subject") continue;
756
+ subjectCount = subjectCount === null ? 0 : subjectCount + 1;
757
+ }
758
+ if (subjectCount === null || metadataIndex === null) return;
759
+ Epub.getXmlChildren(metadata).splice(metadataIndex, 1);
760
+ });
761
+ }
762
+ /**
763
+ * Retrieve the list of subjects for this EPUB.
764
+ *
765
+ * Subjects without associated authority and term metadata
766
+ * will be returned as strings. Otherwise, they will
767
+ * be represented as DcSubject objects, with a value,
768
+ * authority, and term.
769
+ *
770
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dcsubject
771
+ */
772
+ async getSubjects() {
773
+ const metadata = await this.getMetadata();
774
+ const subjectEntries = metadata.filter(({ type }) => type === "dc:subject");
775
+ const subjects = subjectEntries.map(({ value }) => value).filter((value) => !!value);
776
+ metadata.forEach((entry) => {
777
+ if (entry.type !== "meta" || entry.properties["property"] !== "term" && entry.properties["property"] !== "authority") {
778
+ return;
779
+ }
780
+ const subjectIdref = entry.properties["refines"];
781
+ if (!subjectIdref) return;
782
+ const subjectId = subjectIdref.slice(1);
783
+ const index = subjectEntries.findIndex((entry2) => entry2.id === subjectId);
784
+ if (index === -1) return;
785
+ const subject = typeof subjects[index] === "string" ? { value: subjects[index], authority: void 0, term: void 0 } : (
786
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
787
+ subjects[index]
788
+ );
789
+ subject[entry.properties["property"]] = entry.value;
790
+ subjects.splice(index, 1, subject);
791
+ });
792
+ return subjects;
793
+ }
794
+ /**
795
+ * Retrieve the Epub's language as specified in its
796
+ * package document metadata.
797
+ *
798
+ * If no language metadata is specified, returns null.
799
+ * Returns the language as an Intl.Locale instance.
800
+ *
801
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
802
+ */
803
+ async getLanguage() {
804
+ const metadata = await this.getMetadata();
805
+ const languageEntries = metadata.filter(
806
+ (entry) => entry.type === "dc:language"
807
+ );
808
+ const primaryLanguage = languageEntries[0];
809
+ if (!primaryLanguage) return null;
810
+ const locale = primaryLanguage.value;
811
+ if (!locale || locale.toLowerCase() === "und") return null;
812
+ return new Intl.Locale(locale);
813
+ }
814
+ /**
815
+ * Update the Epub's language metadata entry.
816
+ *
817
+ * Updates the existing dc:language element if one exists.
818
+ * Otherwise creates a new element
819
+ *
820
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dclanguage
821
+ */
822
+ async setLanguage(locale) {
823
+ await this.replaceMetadata(({ type }) => type === "dc:language", {
824
+ type: "dc:language",
825
+ properties: {},
826
+ value: locale.toString()
827
+ });
828
+ }
829
+ /**
830
+ * Retrieve the title of the Epub.
831
+ *
832
+ * @param main Optional - whether to return only the first title segment
833
+ * if multiple are found. Otherwise, will follow the spec to combine title
834
+ * segments
835
+ *
836
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
837
+ */
838
+ async getTitle(expanded = false) {
839
+ var _a;
840
+ const entries = await this.getTitles();
841
+ if (!expanded) {
842
+ const mainEntry = entries.find((entry) => entry.type === "main");
843
+ if (mainEntry) return mainEntry.title;
844
+ const shortEntry = entries.find((entry) => entry.type === "short");
845
+ if (shortEntry) return shortEntry.title;
846
+ return ((_a = entries[0]) == null ? void 0 : _a.title) ?? null;
847
+ }
848
+ const expandedEntry = entries.find((entry) => entry.type === "expanded");
849
+ if (expandedEntry) return expandedEntry.title;
850
+ return entries.map((entry) => entry.title).join(", ");
851
+ }
852
+ /**
853
+ * Retrieve the subtitle of the Epub, if it exists.
854
+ *
855
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
856
+ */
857
+ async getSubtitle() {
858
+ const entries = await this.getTitles();
859
+ const subtitleEntry = entries.find((entry) => entry.type === "subtitle");
860
+ return (subtitleEntry == null ? void 0 : subtitleEntry.title) ?? null;
861
+ }
862
+ /**
863
+ * Retrieve all title entries of the Epub.
864
+ *
865
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
866
+ */
867
+ async getTitles() {
868
+ const metadata = await this.getMetadata();
869
+ const titleEntries = metadata.filter((entry) => entry.type === "dc:title");
870
+ const titleRefinements = metadata.filter(
871
+ (entry) => entry.type === "meta" && entry.properties["refines"] && (entry.properties["property"] === "title-type" || entry.properties["property"] === "display-seq")
872
+ );
873
+ const sortedTitleParts = titleEntries.filter(
874
+ (titleEntry) => titleEntry.id && titleRefinements.some(
875
+ (entry) => {
876
+ var _a;
877
+ return entry.value && ((_a = entry.properties["refines"]) == null ? void 0 : _a.slice(1)) === titleEntry.id && entry.properties["property"] === "display-seq" && !Number.isNaN(parseInt(entry.value, 10));
878
+ }
879
+ )
880
+ ).sort((a, b) => {
881
+ const refinementA = titleRefinements.find(
882
+ (entry) => entry.properties["property"] === "display-seq" && entry.properties["refines"].slice(1) === a.id
883
+ );
884
+ const refinementB = titleRefinements.find(
885
+ (entry) => entry.properties["property"] === "display-seq" && entry.properties["refines"].slice(1) === b.id
886
+ );
887
+ const sortA = parseInt(refinementA.value, 10);
888
+ const sortB = parseInt(refinementB.value, 10);
889
+ return sortA - sortB;
890
+ });
891
+ return (sortedTitleParts.length === 0 ? titleEntries : sortedTitleParts).map((entry) => {
892
+ const titleType = titleRefinements.find(
893
+ (refinement) => {
894
+ var _a;
895
+ return ((_a = refinement.properties["refines"]) == null ? void 0 : _a.slice(1)) === entry.id && refinement.properties["property"] === "title-type";
896
+ }
897
+ );
898
+ return {
899
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
900
+ title: entry.value,
901
+ type: (titleType == null ? void 0 : titleType.value) ?? null
902
+ };
903
+ });
904
+ }
905
+ /**
906
+ * Update the Epub's description metadata entry.
907
+ *
908
+ * Updates the existing dc:description element if one exists.
909
+ * Otherwise creates a new element. Any non-ASCII symbols,
910
+ * `&`, `<`, `>`, `"`, `'`, and `\``` will be encoded as HTML entities.
911
+ */
912
+ async setDescription(description) {
913
+ await this.replaceMetadata(({ type }) => type === "dc:description", {
914
+ type: "dc:description",
915
+ value: he.encode(description),
916
+ properties: {}
917
+ });
918
+ }
919
+ /**
920
+ * Retrieve the Epub's description as specified in its
921
+ * package document metadata.
922
+ *
923
+ * If no description metadata is specified, returns null.
924
+ * Returns the description as a string. Descriptions may
925
+ * include HTML markup.
926
+ */
927
+ async getDescription() {
928
+ const metadata = await this.getMetadata();
929
+ const descriptionEntry = metadata.find(
930
+ (entry) => entry.type === "dc:description"
931
+ );
932
+ if (!(descriptionEntry == null ? void 0 : descriptionEntry.value)) return null;
933
+ const escaped = descriptionEntry.value;
934
+ return he.decode(escaped);
935
+ }
936
+ /**
937
+ * Return the set of custom vocabulary prefixes set on this publication's
938
+ * root package element.
939
+ *
940
+ * Returns a map from prefix to URI
941
+ *
942
+ * @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
943
+ */
944
+ async getPackageVocabularyPrefixes() {
945
+ var _a;
946
+ const packageElement = await this.getPackageElement();
947
+ const prefixValue = (_a = packageElement[":@"]) == null ? void 0 : _a["@_prefix"];
948
+ if (!prefixValue) return {};
949
+ const matches = prefixValue.matchAll(/(?:([a-z]+): +(\S+)\s*)/gs);
950
+ return Array.from(matches).reduce(
951
+ (acc, match) => match[1] && match[2] ? { ...acc, [match[1]]: match[2] } : acc,
952
+ {}
953
+ );
954
+ }
955
+ /**
956
+ * Set a custom vocabulary prefix on the root package element.
957
+ *
958
+ * @link https://www.w3.org/TR/epub-33/#sec-prefix-attr
959
+ */
960
+ async setPackageVocabularyPrefix(prefix, uri) {
961
+ await this.withPackage(async (packageElement) => {
962
+ const prefixes = await this.getPackageVocabularyPrefixes();
963
+ prefixes[prefix] = uri;
964
+ packageElement[":@"] ??= {};
965
+ packageElement[":@"]["@_prefix"] = Object.entries(prefixes).map(([p, u]) => `${p}: ${u}`).join("\n ");
966
+ });
967
+ }
968
+ /**
969
+ * Set the title of the Epub.
970
+ *
971
+ * This will replace all existing dc:title elements with
972
+ * this title. It will be given title-type "main".
973
+ *
974
+ * To set specific titles and their types, use epub.setTitles().
975
+ *
976
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dctitle
977
+ */
978
+ // TODO: This should allow users to optionally specify an array,
979
+ // rather than a single string, to support expanded titles.
980
+ async setTitle(title) {
981
+ await this.withPackage((packageElement) => {
982
+ const metadata = Epub.findXmlChildByName(
983
+ "metadata",
984
+ Epub.getXmlChildren(packageElement)
985
+ );
986
+ if (!metadata)
987
+ throw new Error(
988
+ "Failed to parse EPUB: found no metadata element in package document"
989
+ );
990
+ const titleElement = Epub.findXmlChildByName(
991
+ "dc:title",
992
+ metadata.metadata
993
+ );
994
+ if (!titleElement) {
995
+ Epub.getXmlChildren(metadata).push(
996
+ Epub.createXmlElement("dc:title", {}, [
997
+ Epub.createXmlTextNode(title)
998
+ ])
999
+ );
1000
+ } else {
1001
+ titleElement["dc:title"] = [Epub.createXmlTextNode(title)];
1002
+ }
1003
+ });
1004
+ }
1005
+ async setTitles(entries) {
1006
+ await this.withPackage((packageElement) => {
1007
+ var _a, _b;
1008
+ const metadata = Epub.findXmlChildByName(
1009
+ "metadata",
1010
+ Epub.getXmlChildren(packageElement)
1011
+ );
1012
+ if (!metadata) {
1013
+ throw new Error(
1014
+ "Failed to parse EPUB: found no metadata element in package document"
1015
+ );
1016
+ }
1017
+ const metadataEntries = Epub.getXmlChildren(metadata);
1018
+ for (let i = metadataEntries.length - 1; i >= 0; i--) {
1019
+ const meta = metadataEntries[i];
1020
+ if (Epub.isXmlTextNode(meta)) continue;
1021
+ if (Epub.getXmlElementName(meta) === "dc:title" || ((_a = meta[":@"]) == null ? void 0 : _a["@_property"]) === "title-type" || ((_b = meta[":@"]) == null ? void 0 : _b["@_property"]) === "display-seq") {
1022
+ metadataEntries.splice(i, 1);
1023
+ }
1024
+ }
1025
+ for (let i = 0; i < entries.length; i++) {
1026
+ const entry = entries[i];
1027
+ const id = nanoid();
1028
+ metadataEntries.push(
1029
+ Epub.createXmlElement("dc:title", { id }, [
1030
+ Epub.createXmlTextNode(entry.title)
1031
+ ])
1032
+ );
1033
+ if (entry.type) {
1034
+ metadataEntries.push(
1035
+ Epub.createXmlElement(
1036
+ "meta",
1037
+ { refines: `#${id}`, property: "title-type" },
1038
+ [Epub.createXmlTextNode(entry.type)]
1039
+ )
1040
+ );
1041
+ }
1042
+ metadataEntries.push(
1043
+ Epub.createXmlElement(
1044
+ "meta",
1045
+ { refines: `#${id}`, property: "display-seq" },
1046
+ [Epub.createXmlTextNode((i + 1).toString())]
1047
+ )
1048
+ );
1049
+ }
1050
+ });
1051
+ }
1052
+ /**
1053
+ * Retrieve the list of collections.
1054
+ */
1055
+ async getCollections() {
1056
+ var _a, _b;
1057
+ const metadata = await this.getMetadata();
1058
+ const collections = [];
1059
+ for (const entry of metadata) {
1060
+ if (entry.properties["property"] === "belongs-to-collection" && entry.value) {
1061
+ const type = (_a = metadata.find(
1062
+ (e) => e.properties["refines"] === `#${entry.id ?? ""}` && e.properties["property"] === "collection-type"
1063
+ )) == null ? void 0 : _a.value;
1064
+ const position = (_b = metadata.find(
1065
+ (e) => e.properties["refines"] === `#${entry.id ?? ""}` && e.properties["property"] === "group-position"
1066
+ )) == null ? void 0 : _b.value;
1067
+ collections.push({
1068
+ name: entry.value,
1069
+ ...type && { type },
1070
+ ...position && { position }
1071
+ });
1072
+ }
1073
+ }
1074
+ return collections;
1075
+ }
1076
+ /**
1077
+ * Add a collection to the EPUB metadata.
1078
+ *
1079
+ * If index is provided, the collection will be placed at
1080
+ * that index in the list of collections. Otherwise, it
1081
+ * will be added to the end of the list.
1082
+ */
1083
+ async addCollection(collection, index) {
1084
+ const collectionId = nanoid();
1085
+ await this.withPackage((packageElement) => {
1086
+ var _a;
1087
+ const metadata = Epub.findXmlChildByName(
1088
+ "metadata",
1089
+ Epub.getXmlChildren(packageElement)
1090
+ );
1091
+ if (!metadata)
1092
+ throw new Error(
1093
+ "Failed to parse EPUB: found no metadata element in package document"
1094
+ );
1095
+ let collectionCount = 0;
1096
+ let metadataIndex = 0;
1097
+ for (const meta of Epub.getXmlChildren(metadata)) {
1098
+ if (collectionCount === index) break;
1099
+ metadataIndex++;
1100
+ if (Epub.isXmlTextNode(meta)) continue;
1101
+ if (Epub.getXmlElementName(meta) !== "meta") continue;
1102
+ if (((_a = meta[":@"]) == null ? void 0 : _a["@_property"]) !== "belongs-to-collection") continue;
1103
+ collectionCount++;
1104
+ }
1105
+ Epub.getXmlChildren(metadata).splice(
1106
+ metadataIndex,
1107
+ 0,
1108
+ Epub.createXmlElement(
1109
+ "meta",
1110
+ { id: collectionId, property: "belongs-to-collection" },
1111
+ [Epub.createXmlTextNode(collection.name)]
1112
+ )
1113
+ );
1114
+ });
1115
+ if (collection.position) {
1116
+ await this.addMetadata({
1117
+ type: "meta",
1118
+ properties: { refines: `#${collectionId}`, property: "group-position" },
1119
+ value: collection.position
1120
+ });
1121
+ }
1122
+ if (collection.type) {
1123
+ await this.addMetadata({
1124
+ type: "meta",
1125
+ properties: {
1126
+ refines: `#${collectionId}`,
1127
+ property: "collection-type"
1128
+ },
1129
+ value: collection.type
1130
+ });
1131
+ }
1132
+ }
1133
+ /**
1134
+ * Remove a collection from the EPUB metadata.
1135
+ *
1136
+ * Removes the collection at the provided index. This index
1137
+ * refers to the array returned by `epub.getCollections()`.
1138
+ */
1139
+ async removeCollection(index) {
1140
+ await this.withPackage((packageElement) => {
1141
+ var _a, _b;
1142
+ const metadata = Epub.findXmlChildByName(
1143
+ "metadata",
1144
+ Epub.getXmlChildren(packageElement)
1145
+ );
1146
+ if (!metadata)
1147
+ throw new Error(
1148
+ "Failed to parse EPUB: found no metadata element in package document"
1149
+ );
1150
+ let collectionCount = null;
1151
+ let metadataIndex = null;
1152
+ for (const meta of Epub.getXmlChildren(metadata)) {
1153
+ if (collectionCount === index) break;
1154
+ metadataIndex = metadataIndex === null ? 0 : metadataIndex + 1;
1155
+ if (Epub.isXmlTextNode(meta)) continue;
1156
+ if (Epub.getXmlElementName(meta) !== "meta") continue;
1157
+ if (((_a = meta[":@"]) == null ? void 0 : _a["@_property"]) !== "belongs-to-collection") continue;
1158
+ collectionCount = collectionCount === null ? 0 : collectionCount + 1;
1159
+ }
1160
+ if (collectionCount === null || metadataIndex === null) return;
1161
+ const [removed] = Epub.getXmlChildren(metadata).splice(metadataIndex, 1);
1162
+ if (removed && !Epub.isXmlTextNode(removed) && ((_b = removed[":@"]) == null ? void 0 : _b["@_id"])) {
1163
+ const id = removed[":@"]["@_id"];
1164
+ const newChildren = Epub.getXmlChildren(metadata).filter((node) => {
1165
+ var _a2;
1166
+ if (Epub.isXmlTextNode(node)) return true;
1167
+ if (Epub.getXmlElementName(node) !== "meta") return true;
1168
+ if (((_a2 = node[":@"]) == null ? void 0 : _a2["@_refines"]) !== `#${id}`) return true;
1169
+ return false;
1170
+ });
1171
+ Epub.replaceXmlChildren(metadata, newChildren);
1172
+ }
1173
+ });
1174
+ }
1175
+ /**
1176
+ * Retrieve the list of creators.
1177
+ *
1178
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
1179
+ */
1180
+ async getCreators(type = "creator") {
1181
+ const metadata = await this.getMetadata();
1182
+ const creatorEntries = metadata.filter(
1183
+ (entry) => entry.type === `dc:${type}`
1184
+ );
1185
+ const creators = creatorEntries.map(({ value }) => value).filter((value) => !!value).map((value) => ({ name: value }));
1186
+ metadata.forEach((entry) => {
1187
+ if (entry.type !== "meta" || entry.properties["property"] !== "file-as" && entry.properties["property"] !== "role" && entry.properties["property"] !== "alternate-script" || !entry.value) {
1188
+ return;
1189
+ }
1190
+ const creatorIdref = entry.properties["refines"];
1191
+ if (!creatorIdref) return;
1192
+ const creatorId = creatorIdref.slice(1);
1193
+ const index = creatorEntries.findIndex((entry2) => entry2.id === creatorId);
1194
+ if (index === -1) return;
1195
+ const creator = creators[index];
1196
+ if (entry.properties["alternate-script"]) {
1197
+ if (!entry.properties["xml:lang"]) return;
1198
+ creator.alternateScripts ??= [];
1199
+ creator.alternateScripts.push({
1200
+ name: entry.value,
1201
+ locale: new Intl.Locale(entry.properties["xml:lang"])
1202
+ });
1203
+ return;
1204
+ }
1205
+ const prop = entry.properties["property"] === "file-as" ? "fileAs" : "role";
1206
+ creator[prop] = entry.value;
1207
+ if (prop === "role" && entry.properties["scheme"]) {
1208
+ creator.roleScheme = entry.properties["scheme"];
1209
+ }
1210
+ });
1211
+ return creators;
1212
+ }
1213
+ /**
1214
+ * Retrieve the list of contributors.
1215
+ *
1216
+ * This is a convenience method for
1217
+ * `epub.getCreators('contributor')`.
1218
+ *
1219
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccontributor
1220
+ */
1221
+ getContributors() {
1222
+ return this.getCreators("contributor");
1223
+ }
1224
+ /**
1225
+ * Add a creator to the EPUB metadata.
1226
+ *
1227
+ * If index is provided, the creator will be placed at
1228
+ * that index in the list of creators. Otherwise, it
1229
+ * will be added to the end of the list.
1230
+ *
1231
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
1232
+ */
1233
+ async addCreator(creator, index, type = "creator") {
1234
+ const creatorId = nanoid();
1235
+ await this.withPackage((packageElement) => {
1236
+ const metadata = Epub.findXmlChildByName(
1237
+ "metadata",
1238
+ Epub.getXmlChildren(packageElement)
1239
+ );
1240
+ if (!metadata)
1241
+ throw new Error(
1242
+ "Failed to parse EPUB: found no metadata element in package document"
1243
+ );
1244
+ let creatorCount = 0;
1245
+ let metadataIndex = 0;
1246
+ for (const meta of Epub.getXmlChildren(metadata)) {
1247
+ if (creatorCount === index) break;
1248
+ metadataIndex++;
1249
+ if (Epub.isXmlTextNode(meta)) continue;
1250
+ if (Epub.getXmlElementName(meta) !== `dc:${type}`) continue;
1251
+ creatorCount++;
1252
+ }
1253
+ Epub.getXmlChildren(metadata).splice(
1254
+ metadataIndex,
1255
+ 0,
1256
+ Epub.createXmlElement(`dc:${type}`, { id: creatorId }, [
1257
+ Epub.createXmlTextNode(creator.name)
1258
+ ])
1259
+ );
1260
+ });
1261
+ if (creator.role) {
1262
+ await this.addMetadata({
1263
+ type: "meta",
1264
+ properties: {
1265
+ refines: `#${creatorId}`,
1266
+ property: "role",
1267
+ ...creator.roleScheme && { scheme: creator.roleScheme }
1268
+ },
1269
+ value: creator.role
1270
+ });
1271
+ }
1272
+ if (creator.fileAs) {
1273
+ await this.addMetadata({
1274
+ type: "meta",
1275
+ properties: { refines: `#${creatorId}`, property: "file-as" },
1276
+ value: creator.fileAs
1277
+ });
1278
+ }
1279
+ if (creator.alternateScripts) {
1280
+ for (const alternate of creator.alternateScripts) {
1281
+ await this.addMetadata({
1282
+ type: "meta",
1283
+ properties: {
1284
+ refines: `#${creatorId}`,
1285
+ property: "alternate-script",
1286
+ "xml:lang": alternate.locale.toString()
1287
+ },
1288
+ value: alternate.name
1289
+ });
1290
+ }
1291
+ }
1292
+ }
1293
+ /**
1294
+ * Remove a creator from the EPUB metadata.
1295
+ *
1296
+ * Removes the creator at the provided index. This index
1297
+ * refers to the array returned by `epub.getCreators()`.
1298
+ *
1299
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
1300
+ */
1301
+ async removeCreator(index, type = "creator") {
1302
+ await this.withPackage((packageElement) => {
1303
+ var _a;
1304
+ const metadata = Epub.findXmlChildByName(
1305
+ "metadata",
1306
+ Epub.getXmlChildren(packageElement)
1307
+ );
1308
+ if (!metadata)
1309
+ throw new Error(
1310
+ "Failed to parse EPUB: found no metadata element in package document"
1311
+ );
1312
+ let creatorCount = null;
1313
+ let metadataIndex = null;
1314
+ for (const meta of Epub.getXmlChildren(metadata)) {
1315
+ if (creatorCount === index) break;
1316
+ metadataIndex = metadataIndex === null ? 0 : metadataIndex + 1;
1317
+ if (Epub.isXmlTextNode(meta)) continue;
1318
+ if (Epub.getXmlElementName(meta) !== `dc:${type}`) continue;
1319
+ creatorCount = creatorCount === null ? 0 : creatorCount + 1;
1320
+ }
1321
+ if (creatorCount === null || metadataIndex === null) return;
1322
+ const [removed] = Epub.getXmlChildren(metadata).splice(metadataIndex, 1);
1323
+ if (removed && !Epub.isXmlTextNode(removed) && ((_a = removed[":@"]) == null ? void 0 : _a["@_id"])) {
1324
+ const id = removed[":@"]["@_id"];
1325
+ const newChildren = Epub.getXmlChildren(metadata).filter((node) => {
1326
+ var _a2;
1327
+ if (Epub.isXmlTextNode(node)) return true;
1328
+ if (Epub.getXmlElementName(node) !== "meta") return true;
1329
+ if (((_a2 = node[":@"]) == null ? void 0 : _a2["@_refines"]) !== `#${id}`) return true;
1330
+ return false;
1331
+ });
1332
+ Epub.replaceXmlChildren(metadata, newChildren);
1333
+ }
1334
+ });
1335
+ }
1336
+ /**
1337
+ * Remove a contributor from the EPUB metadata.
1338
+ *
1339
+ * Removes the contributor at the provided index. This index
1340
+ * refers to the array returned by `epub.getContributors()`.
1341
+ *
1342
+ * This is a convenience method for
1343
+ * `epub.removeCreator(index, 'contributor')`.
1344
+ *
1345
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
1346
+ */
1347
+ async removeContributor(index) {
1348
+ return this.removeCreator(index, "contributor");
1349
+ }
1350
+ /**
1351
+ * Add a contributor to the EPUB metadata.
1352
+ *
1353
+ * If index is provided, the creator will be placed at
1354
+ * that index in the list of creators. Otherwise, it
1355
+ * will be added to the end of the list.
1356
+ *
1357
+ * This is a convenience method for
1358
+ * `epub.addCreator(contributor, index, 'contributor')`.
1359
+ *
1360
+ * @link https://www.w3.org/TR/epub-33/#sec-opf-dccreator
1361
+ */
1362
+ addContributor(contributor, index) {
1363
+ return this.addCreator(contributor, index, "contributor");
1364
+ }
1365
+ async getSpine() {
1366
+ if (this.spine !== null) return this.spine;
1367
+ const packageElement = await this.getPackageElement();
1368
+ const spine = Epub.findXmlChildByName(
1369
+ "spine",
1370
+ Epub.getXmlChildren(packageElement)
1371
+ );
1372
+ if (!spine)
1373
+ throw new Error(
1374
+ "Failed to parse EPUB: Found no spine element in package document"
1375
+ );
1376
+ this.spine = spine["spine"].filter((node) => !Epub.isXmlTextNode(node)).map((itemref) => {
1377
+ var _a;
1378
+ return (_a = itemref[":@"]) == null ? void 0 : _a["@_idref"];
1379
+ }).filter((idref) => !!idref);
1380
+ return this.spine;
1381
+ }
1382
+ /**
1383
+ * Retrieve the manifest items that make up the Epub's spine.
1384
+ *
1385
+ * The spine specifies the order that the contents of the Epub
1386
+ * should be displayed to users by default.
1387
+ *
1388
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
1389
+ */
1390
+ async getSpineItems() {
1391
+ const spine = await this.getSpine();
1392
+ const manifest = await this.getManifest();
1393
+ return spine.map((itemref) => manifest[itemref]).filter((entry) => !!entry);
1394
+ }
1395
+ /**
1396
+ * Add an item to the spine of the EPUB.
1397
+ *
1398
+ * If `index` is undefined, the item will be added
1399
+ * to the end of the spine. Otherwise it will be
1400
+ * inserted at the specified index.
1401
+ *
1402
+ * If the manifestId does not correspond to an item
1403
+ * in the manifest, this will throw an error.
1404
+ *
1405
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
1406
+ */
1407
+ async addSpineItem(manifestId, index) {
1408
+ const item = Epub.createXmlElement("itemref", { idref: manifestId });
1409
+ const manifest = await this.getManifest();
1410
+ const manifestItem = manifest[manifestId];
1411
+ if (!manifestItem)
1412
+ throw new Error(`Manifest item not found with id "${manifestId}"`);
1413
+ await this.withPackage((packageElement) => {
1414
+ const spine = Epub.findXmlChildByName(
1415
+ "spine",
1416
+ Epub.getXmlChildren(packageElement)
1417
+ );
1418
+ if (!spine)
1419
+ throw new Error(
1420
+ "Failed to parse EPUB: Found no spine element in package document"
1421
+ );
1422
+ if (index === void 0) {
1423
+ Epub.getXmlChildren(spine).push(item);
1424
+ } else {
1425
+ Epub.getXmlChildren(spine).splice(index, 0, item);
1426
+ }
1427
+ });
1428
+ this.spine = null;
1429
+ }
1430
+ /**
1431
+ * Remove the spine item at the specified index.
1432
+ *
1433
+ * @link https://www.w3.org/TR/epub-33/#sec-spine-elem
1434
+ */
1435
+ async removeSpineItem(index) {
1436
+ await this.withPackage((packageElement) => {
1437
+ const spine = Epub.findXmlChildByName(
1438
+ "spine",
1439
+ Epub.getXmlChildren(packageElement)
1440
+ );
1441
+ if (!spine)
1442
+ throw new Error(
1443
+ "Failed to parse EPUB: Found no spine element in package document"
1444
+ );
1445
+ Epub.getXmlChildren(spine).splice(index, 1);
1446
+ });
1447
+ this.spine = null;
1448
+ }
1449
+ /**
1450
+ * Returns a Zip Entry path for an HREF
1451
+ */
1452
+ resolveHref(from, href) {
1453
+ const startPath = dirname(from);
1454
+ const absoluteStartPath = startPath.startsWith("/") ? startPath : `/${startPath}`;
1455
+ return resolve(absoluteStartPath, href).slice(1);
1456
+ }
1457
+ async readItemContents(id, encoding) {
1458
+ const rootfile = await this.getRootfile();
1459
+ const manifest = await this.getManifest();
1460
+ const manifestItem = manifest[id];
1461
+ if (!manifestItem)
1462
+ throw new Error(`Could not find item with id "${id}" in manifest`);
1463
+ const path = this.resolveHref(rootfile, manifestItem.href);
1464
+ const itemEntry = encoding ? await this.getFileData(path, encoding) : await this.getFileData(path);
1465
+ return itemEntry;
1466
+ }
1467
+ /**
1468
+ * Create a new XHTML document with the given body
1469
+ * and head.
1470
+ *
1471
+ * @param body The XML nodes to place in the body of the document
1472
+ * @param head Optional - the XMl nodes to place in the head
1473
+ * @param language Optional - defaults to the EPUB's language
1474
+ */
1475
+ async createXhtmlDocument(body, head, language) {
1476
+ const lang = language ?? await this.getLanguage();
1477
+ return [
1478
+ Epub.createXmlElement("?xml", { version: "1.0", encoding: "UTF-8" }),
1479
+ Epub.createXmlElement(
1480
+ "html",
1481
+ {
1482
+ xmlns: "http://www.w3.org/1999/xhtml",
1483
+ "xmlns:epub": "http://www.idpf.org/2007/ops",
1484
+ ...lang && { "xml:lang": lang.toString(), lang: lang.toString() }
1485
+ },
1486
+ [
1487
+ Epub.createXmlElement("head", {}, head),
1488
+ Epub.createXmlElement("body", {}, body)
1489
+ ]
1490
+ )
1491
+ ];
1492
+ }
1493
+ async readXhtmlItemContents(id, as = "xhtml") {
1494
+ const contents = await this.readItemContents(id, "utf-8");
1495
+ const xml = Epub.xhtmlParser.parse(contents);
1496
+ if (as === "xhtml") return xml;
1497
+ const body = Epub.getXhtmlBody(xml);
1498
+ return Epub.getXhtmlTextContent(body);
1499
+ }
1500
+ writeEntryContents(path, contents, encoding) {
1501
+ const data = encoding === "utf-8" ? new TextEncoder().encode(contents) : contents;
1502
+ const entry = this.getEntry(path);
1503
+ if (!entry) throw new Error(`Could not find file at ${path} in EPUB`);
1504
+ entry.setData(data);
1505
+ }
1506
+ async writeItemContents(id, contents, encoding) {
1507
+ const rootfile = await this.getRootfile();
1508
+ const manifest = await this.getManifest();
1509
+ const manifestItem = manifest[id];
1510
+ if (!manifestItem)
1511
+ throw new Error(`Could not find item with id "${id}" in manifest`);
1512
+ memoize.clear(this.readXhtmlItemContents);
1513
+ const href = this.resolveHref(rootfile, manifestItem.href);
1514
+ if (encoding === "utf-8") {
1515
+ this.writeEntryContents(href, contents, encoding);
1516
+ } else {
1517
+ this.writeEntryContents(href, contents);
1518
+ }
1519
+ }
1520
+ /**
1521
+ * Write new contents for an existing XHTML item,
1522
+ * specified by its id.
1523
+ *
1524
+ * The id must reference an existing manifest item. If
1525
+ * creating a new item, use `epub.addManifestItem()` instead.
1526
+ *
1527
+ * @param id The id of the manifest item to write new contents for
1528
+ * @param contents The new contents. Must be a parsed XML tree.
1529
+ *
1530
+ * @link https://www.w3.org/TR/epub-33/#sec-xhtml
1531
+ */
1532
+ async writeXhtmlItemContents(id, contents) {
1533
+ await this.writeItemContents(
1534
+ id,
1535
+ Epub.xhtmlBuilder.build(contents),
1536
+ "utf-8"
1537
+ );
1538
+ }
1539
+ async removeManifestItem(id) {
1540
+ await this.withPackage(async (packageElement) => {
1541
+ var _a;
1542
+ const manifest = Epub.findXmlChildByName(
1543
+ "manifest",
1544
+ Epub.getXmlChildren(packageElement)
1545
+ );
1546
+ if (!manifest)
1547
+ throw new Error(
1548
+ "Failed to parse EPUB: Found no manifest element in package document"
1549
+ );
1550
+ const itemIndex = Epub.getXmlChildren(manifest).findIndex(
1551
+ (node) => {
1552
+ var _a2;
1553
+ return !Epub.isXmlTextNode(node) && ((_a2 = node[":@"]) == null ? void 0 : _a2["@_id"]) === id;
1554
+ }
1555
+ );
1556
+ if (itemIndex === -1) return;
1557
+ const [item] = Epub.getXmlChildren(manifest).splice(itemIndex, 1);
1558
+ if (!item || Epub.isXmlTextNode(item) || !((_a = item[":@"]) == null ? void 0 : _a["@_href"])) return;
1559
+ await this.removeEntry(item[":@"]["@_href"]);
1560
+ });
1561
+ this.manifest = null;
1562
+ }
1563
+ async addManifestItem(item, contents, encoding) {
1564
+ await this.withPackage((packageElement) => {
1565
+ const manifest = Epub.findXmlChildByName(
1566
+ "manifest",
1567
+ Epub.getXmlChildren(packageElement)
1568
+ );
1569
+ if (!manifest)
1570
+ throw new Error(
1571
+ "Failed to parse EPUB: Found no manifest element in package document"
1572
+ );
1573
+ Epub.getXmlChildren(manifest).push(
1574
+ Epub.createXmlElement("item", {
1575
+ id: item.id,
1576
+ href: item.href,
1577
+ ...item.mediaType && { "media-type": item.mediaType },
1578
+ ...item.fallback && { fallback: item.fallback },
1579
+ ...item.mediaOverlay && { "media-overlay": item.mediaOverlay },
1580
+ ...item.properties && { properties: item.properties.join(" ") }
1581
+ })
1582
+ );
1583
+ });
1584
+ this.manifest = null;
1585
+ const rootfile = await this.getRootfile();
1586
+ const filename = this.resolveHref(rootfile, item.href);
1587
+ const data = encoding === "utf-8" || encoding === "xml" ? new TextEncoder().encode(
1588
+ encoding === "utf-8" ? contents : await Epub.xmlBuilder.build(
1589
+ contents
1590
+ )
1591
+ ) : contents;
1592
+ this.entries.push(new EpubEntry({ filename, data }));
1593
+ }
1594
+ /**
1595
+ * Update the manifest entry for an existing item.
1596
+ *
1597
+ * To update the contents of an entry, use `epub.writeItemContents()`
1598
+ * or `epub.writeXhtmlItemContents()`
1599
+ *
1600
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-manifest
1601
+ */
1602
+ async updateManifestItem(id, newItem) {
1603
+ await this.withPackage((packageElement) => {
1604
+ const manifest = Epub.findXmlChildByName(
1605
+ "manifest",
1606
+ Epub.getXmlChildren(packageElement)
1607
+ );
1608
+ if (!manifest)
1609
+ throw new Error(
1610
+ "Failed to parse EPUB: Found no manifest element in package document"
1611
+ );
1612
+ const itemIndex = manifest["manifest"].findIndex(
1613
+ (item) => {
1614
+ var _a;
1615
+ return !Epub.isXmlTextNode(item) && ((_a = item[":@"]) == null ? void 0 : _a["@_id"]) === id;
1616
+ }
1617
+ );
1618
+ Epub.getXmlChildren(manifest).splice(
1619
+ itemIndex,
1620
+ 1,
1621
+ Epub.createXmlElement("item", {
1622
+ id,
1623
+ href: newItem.href,
1624
+ ...newItem.mediaType && { "media-type": newItem.mediaType },
1625
+ ...newItem.fallback && { fallback: newItem.fallback },
1626
+ ...newItem.mediaOverlay && {
1627
+ "media-overlay": newItem.mediaOverlay
1628
+ },
1629
+ ...newItem.properties && {
1630
+ properties: newItem.properties.join(" ")
1631
+ }
1632
+ })
1633
+ );
1634
+ });
1635
+ this.manifest = null;
1636
+ }
1637
+ /**
1638
+ * Add a new metadata entry to the Epub.
1639
+ *
1640
+ * This method, like `epub.getMetadata()`, operates on
1641
+ * metadata entries. For more useful semantic representations
1642
+ * of metadata, use specific methods such as `setTitle()` and
1643
+ * `setLanguage()`.
1644
+ *
1645
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
1646
+ */
1647
+ async addMetadata(entry) {
1648
+ await this.withPackage((packageElement) => {
1649
+ const metadata = Epub.findXmlChildByName(
1650
+ "metadata",
1651
+ Epub.getXmlChildren(packageElement)
1652
+ );
1653
+ if (!metadata)
1654
+ throw new Error(
1655
+ "Failed to parse EPUB: found no metadata element in package document"
1656
+ );
1657
+ Epub.getXmlChildren(metadata).push(
1658
+ Epub.createXmlElement(
1659
+ entry.type,
1660
+ {
1661
+ ...entry.id && { id: entry.id },
1662
+ ...entry.properties
1663
+ },
1664
+ entry.value !== void 0 ? [Epub.createXmlTextNode(entry.value)] : []
1665
+ )
1666
+ );
1667
+ });
1668
+ }
1669
+ /**
1670
+ * Replace a metadata entry with a new one.
1671
+ *
1672
+ * The `predicate` argument will be used to determine which entry
1673
+ * to replace. The first metadata entry that matches the
1674
+ * predicate will be replaced.
1675
+ *
1676
+ * @param predicate Calls predicate once for each metadata entry,
1677
+ * until it finds one where predicate returns true
1678
+ * @param entry The new entry to replace the found entry with
1679
+ *
1680
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
1681
+ */
1682
+ async replaceMetadata(predicate, entry) {
1683
+ await this.withPackage((packageElement) => {
1684
+ const metadataElement = Epub.findXmlChildByName(
1685
+ "metadata",
1686
+ Epub.getXmlChildren(packageElement)
1687
+ );
1688
+ if (!metadataElement)
1689
+ throw new Error(
1690
+ "Failed to parse EPUB: found no metadata element in package document"
1691
+ );
1692
+ const oldEntryIndex = this.findMetadataIndex(packageElement, predicate);
1693
+ const newElement = Epub.createXmlElement(
1694
+ entry.type,
1695
+ {
1696
+ ...entry.id && { id: entry.id },
1697
+ ...entry.properties
1698
+ },
1699
+ entry.value !== void 0 ? [Epub.createXmlTextNode(entry.value)] : []
1700
+ );
1701
+ if (oldEntryIndex === -1) {
1702
+ metadataElement.metadata.push(newElement);
1703
+ } else {
1704
+ metadataElement.metadata.splice(oldEntryIndex, 1, newElement);
1705
+ }
1706
+ });
1707
+ }
1708
+ /**
1709
+ * Remove one or more metadata entries.
1710
+ *
1711
+ * The `predicate` argument will be used to determine which entries
1712
+ * to remove. The all metadata entries that match the
1713
+ * predicate will be removed.
1714
+ *
1715
+ * @param predicate Calls predicate once for each metadata entry,
1716
+ * removing any for which it returns true
1717
+ *
1718
+ * @link https://www.w3.org/TR/epub-33/#sec-pkg-metadata
1719
+ */
1720
+ async removeMetadata(predicate) {
1721
+ await this.withPackage((packageElement) => {
1722
+ const metadataElement = Epub.findXmlChildByName(
1723
+ "metadata",
1724
+ Epub.getXmlChildren(packageElement)
1725
+ );
1726
+ if (!metadataElement) {
1727
+ throw new Error(
1728
+ "Failed to parse EPUB: found no metadata element in package document"
1729
+ );
1730
+ }
1731
+ const metadataEntries = Epub.getXmlChildren(metadataElement);
1732
+ for (let i = metadataEntries.length - 1; i >= 0; i--) {
1733
+ const meta = metadataEntries[i];
1734
+ const item = Epub.parseMetadataItem(meta);
1735
+ if (!item) continue;
1736
+ if (predicate(item)) {
1737
+ metadataEntries.splice(i, 1);
1738
+ }
1739
+ }
1740
+ });
1741
+ }
1742
+ /**
1743
+ * Write the current contents of the Epub to a new
1744
+ * Uint8Array.
1745
+ *
1746
+ * This _does not_ close the Epub. It can continue to
1747
+ * be modified after it has been written to disk. Use
1748
+ * `epub.close()` to close the Epub.
1749
+ *
1750
+ * When this method is called, the "dcterms:modified"
1751
+ * meta tag is automatically updated to the current UTC
1752
+ * timestamp.
1753
+ */
1754
+ async writeToArray() {
1755
+ await this.replaceMetadata(
1756
+ (entry) => entry.properties["property"] === "dcterms:modified",
1757
+ {
1758
+ type: "meta",
1759
+ properties: { property: "dcterms:modified" },
1760
+ // We need UTC with integer seconds, but toISOString gives UTC with ms
1761
+ value: (/* @__PURE__ */ new Date()).toISOString().replace(/\.\d+/, "")
1762
+ }
1763
+ );
1764
+ let mimetypeEntry = this.getEntry("mimetype");
1765
+ if (!mimetypeEntry) {
1766
+ mimetypeEntry = new EpubEntry({
1767
+ filename: "mimetype",
1768
+ data: new TextEncoder().encode("application/epub+zip")
1769
+ });
1770
+ this.entries.push(mimetypeEntry);
1771
+ }
1772
+ const mimetypeReader = new Uint8ArrayReader(await mimetypeEntry.getData());
1773
+ try {
1774
+ await this.zipWriter.add(mimetypeEntry.filename, mimetypeReader, {
1775
+ level: 0,
1776
+ extendedTimestamp: false
1777
+ });
1778
+ } catch (e) {
1779
+ if (e instanceof Error && e.message === ERR_DUPLICATED_NAME) {
1780
+ throw new Error(
1781
+ `Failed to add file "${mimetypeEntry.filename}" to zip archive: ${e.message}`
1782
+ );
1783
+ }
1784
+ throw e;
1785
+ }
1786
+ await Promise.all(
1787
+ this.entries.map(async (entry) => {
1788
+ if (entry.filename === "mimetype") return;
1789
+ const reader = new Uint8ArrayReader(await entry.getData());
1790
+ try {
1791
+ return await this.zipWriter.add(entry.filename, reader);
1792
+ } catch (e) {
1793
+ if (e instanceof Error && e.message === ERR_DUPLICATED_NAME) {
1794
+ throw new Error(
1795
+ `Failed to add file "${entry.filename}" to zip archive: ${e.message}`
1796
+ );
1797
+ }
1798
+ throw e;
1799
+ }
1800
+ })
1801
+ );
1802
+ const data = await this.zipWriter.close();
1803
+ this.dataWriter = new Uint8ArrayWriter();
1804
+ this.zipWriter = new ZipWriter(this.dataWriter);
1805
+ return data;
1806
+ }
1807
+ }
1808
+ export {
1809
+ Epub
1810
+ };