@epubook/core 0.0.11 → 0.0.12-beta.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@epubook/core",
3
- "version": "0.0.11",
3
+ "version": "0.0.12-beta.2",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ebook",
@@ -19,35 +19,30 @@
19
19
  "license": "MIT",
20
20
  "author": "XLor",
21
21
  "sideEffects": false,
22
+ "type": "module",
22
23
  "exports": {
23
24
  ".": {
24
- "require": "./dist/index.cjs",
25
- "import": "./dist/index.mjs",
26
- "types": "./dist/index.d.ts"
25
+ "types": "./dist/index.d.mts",
26
+ "import": "./dist/index.mjs"
27
27
  }
28
28
  },
29
- "main": "./dist/index.cjs",
30
29
  "module": "./dist/index.mjs",
31
- "types": "./dist/index.d.ts",
30
+ "types": "./dist/index.d.mts",
32
31
  "files": [
33
32
  "dist"
34
33
  ],
35
34
  "dependencies": {
36
- "defu": "^6.1.2",
37
- "fast-xml-parser": "^4.2.7",
38
- "fflate": "^0.8.0",
39
- "pathe": "^1.1.1"
40
- },
41
- "devDependencies": {
42
- "rollup": "^3.27.2",
43
- "vitest": "^0.33.0"
35
+ "defu": "^6.1.4",
36
+ "fast-xml-parser": "^5.3.3",
37
+ "fflate": "^0.8.2",
38
+ "pathe": "^2.0.3",
39
+ "@epubook/xml": "0.0.12-beta.2"
44
40
  },
45
41
  "engines": {
46
- "node": ">=v18.16.0"
42
+ "node": ">=v20.10.0"
47
43
  },
48
44
  "scripts": {
49
- "build": "unbuild",
50
- "format": "prettier --write src/**/*.{ts,tsx} test/**/*.{ts,tsx}",
45
+ "build": "tsdown",
51
46
  "test": "vitest",
52
47
  "test:ci": "vitest --run",
53
48
  "typecheck": "tsc --noEmit"
@@ -1,190 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('pathe');
4
- const fflate = require('fflate');
5
- const fastXmlParser = require('fast-xml-parser');
6
- const index = require('../index.cjs');
7
- require('node:path');
8
- require('node:crypto');
9
- require('defu');
10
- require('node:fs');
11
-
12
- function _interopNamespaceCompat(e) {
13
- if (e && typeof e === 'object' && 'default' in e) return e;
14
- const n = Object.create(null);
15
- if (e) {
16
- for (const k in e) {
17
- n[k] = e[k];
18
- }
19
- }
20
- n.default = e;
21
- return n;
22
- }
23
-
24
- const path__namespace = /*#__PURE__*/_interopNamespaceCompat(path);
25
- const fflate__namespace = /*#__PURE__*/_interopNamespaceCompat(fflate);
26
-
27
- function toISO8601String(date) {
28
- function pad(n) {
29
- if (n < 10) {
30
- return "0" + n;
31
- }
32
- return n;
33
- }
34
- return date.getUTCFullYear() + "-" + pad(date.getUTCMonth() + 1) + "-" + pad(date.getUTCDate()) + "T" + pad(date.getUTCHours()) + ":" + pad(date.getUTCMinutes()) + ":" + pad(date.getUTCSeconds()) + "Z";
35
- }
36
-
37
- async function bundle(epub) {
38
- return new Promise(async (res, rej) => {
39
- const opfs = epub.packages().map((opf) => [opf.filename(), fflate__namespace.strToU8(makePackageDocument(opf))]);
40
- const items = {};
41
- for (const opf of epub.packages()) {
42
- const base = path__namespace.dirname(opf.filename());
43
- for (const item of opf.items()) {
44
- const name = path__namespace.join(base, item.filename());
45
- if (name in items) {
46
- continue;
47
- }
48
- items[name] = await item.bundle();
49
- }
50
- }
51
- const abstractContainer = {
52
- mimetype: fflate__namespace.strToU8(index.MIMETYPE),
53
- "META-INF": {
54
- "container.xml": fflate__namespace.strToU8(makeContainer(epub))
55
- },
56
- ...Object.fromEntries(opfs),
57
- ...items
58
- };
59
- fflate__namespace.zip(
60
- abstractContainer,
61
- {
62
- level: 0,
63
- mtime: /* @__PURE__ */ new Date()
64
- },
65
- (err, data) => {
66
- if (err) {
67
- rej(err);
68
- } else {
69
- res(data);
70
- }
71
- }
72
- );
73
- });
74
- }
75
- function makeContainer(epub) {
76
- const builder = new fastXmlParser.XMLBuilder({
77
- format: true,
78
- ignoreAttributes: false,
79
- suppressUnpairedNode: false,
80
- unpairedTags: ["rootfile"]
81
- });
82
- const rootfile = epub.packages().map((p) => ({
83
- "@_full-path": p.filename(),
84
- "@_media-type": "application/oebps-package+xml",
85
- "#text": ""
86
- }));
87
- return builder.build({
88
- "?xml": { "#text": "", "@_version": "1.0", "@_encoding": "UTF-8" },
89
- container: {
90
- "@_version": "1.0",
91
- "@_xmlns": "urn:oasis:names:tc:opendocument:xmlns:container",
92
- rootfiles: [
93
- {
94
- rootfile
95
- }
96
- ]
97
- }
98
- });
99
- }
100
- function makePackageDocument(opf) {
101
- if (opf.version() !== "3.0") {
102
- throw new index.BundleError(`Unsupport EPUB spec ${opf.version()}`);
103
- }
104
- const builder = new fastXmlParser.XMLBuilder({
105
- format: true,
106
- ignoreAttributes: false,
107
- suppressUnpairedNode: false,
108
- unpairedTags: ["item", "itemref"]
109
- });
110
- const optionalMetadata = {};
111
- const optionalList = [
112
- "contributor",
113
- "coverage",
114
- "format",
115
- "publisher",
116
- "relation",
117
- "rights",
118
- "source",
119
- "subject",
120
- "type"
121
- ];
122
- for (const key of optionalList) {
123
- const m = opf.metadata();
124
- if (!!m[key]) {
125
- optionalMetadata["dc:" + key] = m[key];
126
- }
127
- }
128
- const metadata = {
129
- "@_xmlns:dc": "http://purl.org/dc/elements/1.1/",
130
- "dc:identifier": {
131
- "@_id": opf.uniqueIdentifier(),
132
- "#text": opf.identifier()
133
- },
134
- "dc:title": opf.title(),
135
- "dc:language": opf.language(),
136
- "dc:creator": {
137
- "@_id": opf.creator().uid,
138
- "#text": opf.creator().name
139
- },
140
- "dc:date": toISO8601String(opf.metadata().date),
141
- "dc:description": opf.metadata().description,
142
- ...optionalMetadata,
143
- meta: [
144
- {
145
- "@_property": "dcterms:modified",
146
- "#text": toISO8601String(opf.metadata().lastModified)
147
- },
148
- {
149
- "@_refines": "#" + opf.creator().uid,
150
- "@_property": "file-as",
151
- "#text": opf.creator()?.fileAs ?? opf.creator().name
152
- }
153
- ]
154
- };
155
- function makeManifestItem(item) {
156
- return {
157
- "@_fallback": item.fallback(),
158
- "@_href": item.href(),
159
- "@_id": item.id(),
160
- "@_media-overlay": item.mediaOverlay(),
161
- "@_media-type": item.mediaType(),
162
- "@_properties": item.properties()
163
- };
164
- }
165
- function makeManifestItemRef(item) {
166
- return {
167
- "@_idref": item.idref()
168
- };
169
- }
170
- return builder.build({
171
- "?xml": { "#text": "", "@_version": "1.0", "@_encoding": "UTF-8" },
172
- package: {
173
- "@_xmlns": "http://www.idpf.org/2007/opf",
174
- "@_xmlns:epub": "http://www.idpf.org/2007/ops",
175
- "@_unique-identifier": opf.uniqueIdentifier(),
176
- "@_version": opf.version(),
177
- metadata,
178
- manifest: {
179
- item: opf.manifest().map((i) => makeManifestItem(i))
180
- },
181
- spine: {
182
- itemref: opf.spine().map((ir) => makeManifestItemRef(ir))
183
- }
184
- }
185
- });
186
- }
187
-
188
- exports.bundle = bundle;
189
- exports.makeContainer = makeContainer;
190
- exports.makePackageDocument = makePackageDocument;
@@ -1,171 +0,0 @@
1
- import * as path from 'pathe';
2
- import * as fflate from 'fflate';
3
- import { XMLBuilder } from 'fast-xml-parser';
4
- import { MIMETYPE, BundleError } from '../index.mjs';
5
- import 'node:path';
6
- import 'node:crypto';
7
- import 'defu';
8
- import 'node:fs';
9
-
10
- function toISO8601String(date) {
11
- function pad(n) {
12
- if (n < 10) {
13
- return "0" + n;
14
- }
15
- return n;
16
- }
17
- return date.getUTCFullYear() + "-" + pad(date.getUTCMonth() + 1) + "-" + pad(date.getUTCDate()) + "T" + pad(date.getUTCHours()) + ":" + pad(date.getUTCMinutes()) + ":" + pad(date.getUTCSeconds()) + "Z";
18
- }
19
-
20
- async function bundle(epub) {
21
- return new Promise(async (res, rej) => {
22
- const opfs = epub.packages().map((opf) => [opf.filename(), fflate.strToU8(makePackageDocument(opf))]);
23
- const items = {};
24
- for (const opf of epub.packages()) {
25
- const base = path.dirname(opf.filename());
26
- for (const item of opf.items()) {
27
- const name = path.join(base, item.filename());
28
- if (name in items) {
29
- continue;
30
- }
31
- items[name] = await item.bundle();
32
- }
33
- }
34
- const abstractContainer = {
35
- mimetype: fflate.strToU8(MIMETYPE),
36
- "META-INF": {
37
- "container.xml": fflate.strToU8(makeContainer(epub))
38
- },
39
- ...Object.fromEntries(opfs),
40
- ...items
41
- };
42
- fflate.zip(
43
- abstractContainer,
44
- {
45
- level: 0,
46
- mtime: /* @__PURE__ */ new Date()
47
- },
48
- (err, data) => {
49
- if (err) {
50
- rej(err);
51
- } else {
52
- res(data);
53
- }
54
- }
55
- );
56
- });
57
- }
58
- function makeContainer(epub) {
59
- const builder = new XMLBuilder({
60
- format: true,
61
- ignoreAttributes: false,
62
- suppressUnpairedNode: false,
63
- unpairedTags: ["rootfile"]
64
- });
65
- const rootfile = epub.packages().map((p) => ({
66
- "@_full-path": p.filename(),
67
- "@_media-type": "application/oebps-package+xml",
68
- "#text": ""
69
- }));
70
- return builder.build({
71
- "?xml": { "#text": "", "@_version": "1.0", "@_encoding": "UTF-8" },
72
- container: {
73
- "@_version": "1.0",
74
- "@_xmlns": "urn:oasis:names:tc:opendocument:xmlns:container",
75
- rootfiles: [
76
- {
77
- rootfile
78
- }
79
- ]
80
- }
81
- });
82
- }
83
- function makePackageDocument(opf) {
84
- if (opf.version() !== "3.0") {
85
- throw new BundleError(`Unsupport EPUB spec ${opf.version()}`);
86
- }
87
- const builder = new XMLBuilder({
88
- format: true,
89
- ignoreAttributes: false,
90
- suppressUnpairedNode: false,
91
- unpairedTags: ["item", "itemref"]
92
- });
93
- const optionalMetadata = {};
94
- const optionalList = [
95
- "contributor",
96
- "coverage",
97
- "format",
98
- "publisher",
99
- "relation",
100
- "rights",
101
- "source",
102
- "subject",
103
- "type"
104
- ];
105
- for (const key of optionalList) {
106
- const m = opf.metadata();
107
- if (!!m[key]) {
108
- optionalMetadata["dc:" + key] = m[key];
109
- }
110
- }
111
- const metadata = {
112
- "@_xmlns:dc": "http://purl.org/dc/elements/1.1/",
113
- "dc:identifier": {
114
- "@_id": opf.uniqueIdentifier(),
115
- "#text": opf.identifier()
116
- },
117
- "dc:title": opf.title(),
118
- "dc:language": opf.language(),
119
- "dc:creator": {
120
- "@_id": opf.creator().uid,
121
- "#text": opf.creator().name
122
- },
123
- "dc:date": toISO8601String(opf.metadata().date),
124
- "dc:description": opf.metadata().description,
125
- ...optionalMetadata,
126
- meta: [
127
- {
128
- "@_property": "dcterms:modified",
129
- "#text": toISO8601String(opf.metadata().lastModified)
130
- },
131
- {
132
- "@_refines": "#" + opf.creator().uid,
133
- "@_property": "file-as",
134
- "#text": opf.creator()?.fileAs ?? opf.creator().name
135
- }
136
- ]
137
- };
138
- function makeManifestItem(item) {
139
- return {
140
- "@_fallback": item.fallback(),
141
- "@_href": item.href(),
142
- "@_id": item.id(),
143
- "@_media-overlay": item.mediaOverlay(),
144
- "@_media-type": item.mediaType(),
145
- "@_properties": item.properties()
146
- };
147
- }
148
- function makeManifestItemRef(item) {
149
- return {
150
- "@_idref": item.idref()
151
- };
152
- }
153
- return builder.build({
154
- "?xml": { "#text": "", "@_version": "1.0", "@_encoding": "UTF-8" },
155
- package: {
156
- "@_xmlns": "http://www.idpf.org/2007/opf",
157
- "@_xmlns:epub": "http://www.idpf.org/2007/ops",
158
- "@_unique-identifier": opf.uniqueIdentifier(),
159
- "@_version": opf.version(),
160
- metadata,
161
- manifest: {
162
- item: opf.manifest().map((i) => makeManifestItem(i))
163
- },
164
- spine: {
165
- itemref: opf.spine().map((ir) => makeManifestItemRef(ir))
166
- }
167
- }
168
- });
169
- }
170
-
171
- export { bundle, makeContainer, makePackageDocument };