Skip to content

Commit

Permalink
feat: init bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 26, 2023
1 parent 225b0f4 commit b9162c7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"fast-xml-parser": "^4.1.2",
"fflate": "^0.7.4"
},
"devDependencies": {
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as fflate from 'fflate';
import { XMLBuilder } from 'fast-xml-parser';

import type { Epubook } from './epub';

const MIMETYPE = fflate.strToU8('application/epub+zip');

export async function bundle(epub: Epubook) {
return new Promise((res, rej) => {
fflate.zip(
{
mimetype: MIMETYPE,
'META-INF': {
'container.xml': fflate.strToU8('')
}
},
{
level: 1,
mtime: new Date()
},
(err, data) => {
if (err) {
rej(err);
} else {
res(data);
}
}
);
});
}

export function makeContainer(epub: Epubook) {
const builder = new XMLBuilder({ format: true, ignoreAttributes: false });
return builder.build({
'?xml': [{ '#text': '' }],
container: {
version: '1.0',
xmlns: 'urn:oasis:names:tc:opendocument:xmlns:container'
}
});
}
8 changes: 8 additions & 0 deletions packages/core/src/epub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Epubook {
constructor() {}

async bundle() {
const { bundle } = await import('./bundle');
return await bundle(this);
}
}
11 changes: 11 additions & 0 deletions packages/core/test/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it, expect } from 'vitest';

import { Epubook } from '../src/epub';
import { makeContainer } from '../src/bundle';

describe('Bundle Epub', () => {
it('generate container.xml', () => {
const res = makeContainer(new Epubook());
console.log(res);
});
});
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b9162c7

Please sign in to comment.