From 9571357a3739efce7cbc4aa314c0d20d664b26d1 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Mon, 4 Nov 2019 09:40:48 -0600 Subject: [PATCH] Add `hasDRM()` and update typings (#44) * add drm detector and update types * fix declaration file * fix declaration again --- epub.d.ts | 94 +++++++++++++++++++++++++++++++++++-------------------- epub.js | 10 ++++++ 2 files changed, 70 insertions(+), 34 deletions(-) diff --git a/epub.d.ts b/epub.d.ts index 7c813e0..c99c827 100644 --- a/epub.d.ts +++ b/epub.d.ts @@ -3,7 +3,7 @@ // Definitions by: Julien Chaumond // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// +/// /** * new EPub(fname[, imageroot][, linkroot]) @@ -30,37 +30,63 @@ * * /images/logo_img/OPT/logo.jpg **/ -declare module "epub" { - - import {EventEmitter} from "events"; - - interface TocElement { - level: number; - order: number; - title: string; - id: string; - href?: string; - } - - class EPub extends EventEmitter { - constructor(epubfile: string, imagewebroot?: string, chapterwebroot?: string); - - metadata: Object; - manifest: Object; - spine: Object; - flow: Array; - toc: Array; - - parse(): void; - - getChapter(chapterId: string, callback: (error: Error, text: string) => void): void; - - getChapterRaw(chapterId: string, callback: (error: Error, text: string) => void): void; - - getImage(id: string, callback: (error: Error, data: Buffer, mimeType: string) => void): void; - - getFile(id: string, callback: (error: Error, data: Buffer, mimeType: string) => void): void; - } - - export = EPub; +import { EventEmitter } from 'events' + +declare class EPub extends EventEmitter { + constructor(epubfile: string, imagewebroot?: string, chapterwebroot?: string) + + metadata: EPub.Metadata + manifest: Object + spine: { + toc: { href: string; id: string } + contents: Array + } + flow: Array + toc: Array + + parse(): void + + getChapter( + chapterId: string, + callback: (error: Error, text: string) => void + ): void + + getChapterRaw( + chapterId: string, + callback: (error: Error, text: string) => void + ): void + + getImage( + id: string, + callback: (error: Error, data: Buffer, mimeType: string) => void + ): void + + getFile( + id: string, + callback: (error: Error, data: Buffer, mimeType: string) => void + ): void + + hasDRM(): boolean +} + +export = EPub + +declare namespace EPub { + export interface TocElement { + level: number + order: number + title: string + id: string + href: string + } + + export interface Metadata { + creator: string + creatorFileAs: string + title: string + language: string + subject: string + date: string + description: string + } } diff --git a/epub.js b/epub.js index ccb7940..f7e9c78 100644 --- a/epub.js +++ b/epub.js @@ -800,6 +800,16 @@ class EPub extends EventEmitter { } }; + /** + * EPub#hasDRM() -> boolean + * + * Parses the tree to see if there's an ecnryption file, signifying the presence of DRM + * see: https://stackoverflow.com/questions/14442968/how-to-check-if-an-epub-file-is-drm-protected + **/ + hasDRM () { + const drmFile = 'META-INF/encryption.xml'; + return this.zip.names.includes(drmFile); + }; } // Expose to the world