Merge pull request #90 from dolanmiu/feat/buffer-packer
Feat/buffer packer
This commit is contained in:
20
demo/demo19.js
Normal file
20
demo/demo19.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
const fs = require("fs");
|
||||||
|
const docx = require("../build");
|
||||||
|
|
||||||
|
var doc = new docx.Document();
|
||||||
|
|
||||||
|
var paragraph = new docx.Paragraph("Hello World");
|
||||||
|
var institutionText = new docx.TextRun("Foo").bold();
|
||||||
|
var dateText = new docx.TextRun("Bar").tab().bold();
|
||||||
|
paragraph.addRun(institutionText);
|
||||||
|
paragraph.addRun(dateText);
|
||||||
|
|
||||||
|
doc.addParagraph(paragraph);
|
||||||
|
|
||||||
|
var exporter = new docx.BufferPacker(doc);
|
||||||
|
exporter.pack("My Document").then((buffer) => {
|
||||||
|
// At this point, you can do anything with the buffer, including casting it to a string etc.
|
||||||
|
console.log(buffer);
|
||||||
|
fs.writeFileSync('My Document.docx', buffer);
|
||||||
|
console.log("Document created successfully at project root!");
|
||||||
|
});
|
@ -2,3 +2,4 @@ export * from "./packer/local";
|
|||||||
export * from "./packer/express";
|
export * from "./packer/express";
|
||||||
export * from "./packer/packer";
|
export * from "./packer/packer";
|
||||||
export * from "./packer/stream";
|
export * from "./packer/stream";
|
||||||
|
export * from "./packer/buffer";
|
||||||
|
28
src/export/packer/buffer-stream.ts
Normal file
28
src/export/packer/buffer-stream.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { Writable } from "stream";
|
||||||
|
|
||||||
|
export class BufferStream extends Writable {
|
||||||
|
private data: Buffer[];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.data = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
public _write(chunk: any, encoding: string, next: (err?: Error) => void): void {
|
||||||
|
this.data.push(Buffer.from(chunk));
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
// tslint:disable-next-line:ban-types
|
||||||
|
public end(cb?: Function): void {
|
||||||
|
super.end(cb);
|
||||||
|
|
||||||
|
this.emit("close");
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Buffer(): Buffer {
|
||||||
|
return Buffer.concat(this.data);
|
||||||
|
}
|
||||||
|
}
|
44
src/export/packer/buffer.spec.ts
Normal file
44
src/export/packer/buffer.spec.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* tslint:disable:typedef space-before-function-paren */
|
||||||
|
import { assert } from "chai";
|
||||||
|
import { stub } from "sinon";
|
||||||
|
|
||||||
|
import { BufferPacker } from "../../export/packer/buffer";
|
||||||
|
import { File, Paragraph } from "../../file";
|
||||||
|
|
||||||
|
describe.only("BufferPacker", () => {
|
||||||
|
let packer: BufferPacker;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const file = new File({
|
||||||
|
creator: "Dolan Miu",
|
||||||
|
revision: "1",
|
||||||
|
lastModifiedBy: "Dolan Miu",
|
||||||
|
});
|
||||||
|
const paragraph = new Paragraph("test text");
|
||||||
|
const heading = new Paragraph("Hello world").heading1();
|
||||||
|
file.addParagraph(new Paragraph("title").title());
|
||||||
|
file.addParagraph(heading);
|
||||||
|
file.addParagraph(new Paragraph("heading 2").heading2());
|
||||||
|
file.addParagraph(paragraph);
|
||||||
|
|
||||||
|
packer = new BufferPacker(file);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#pack()", () => {
|
||||||
|
it("should create a standard docx file", async function() {
|
||||||
|
this.timeout(99999999);
|
||||||
|
const buffer = await packer.pack();
|
||||||
|
assert.isDefined(buffer);
|
||||||
|
assert.isTrue(buffer.byteLength > 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle exception if it throws any", () => {
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
const compiler = stub((packer as any).packer, "compile");
|
||||||
|
compiler.throwsException();
|
||||||
|
return packer.pack().catch((error) => {
|
||||||
|
assert.isDefined(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
19
src/export/packer/buffer.ts
Normal file
19
src/export/packer/buffer.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { File } from "../../file";
|
||||||
|
import { BufferStream } from "./buffer-stream";
|
||||||
|
import { Compiler } from "./compiler";
|
||||||
|
|
||||||
|
export class BufferPacker {
|
||||||
|
private readonly packer: Compiler;
|
||||||
|
|
||||||
|
constructor(file: File) {
|
||||||
|
this.packer = new Compiler(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async pack(): Promise<Buffer> {
|
||||||
|
const stream = new BufferStream();
|
||||||
|
|
||||||
|
await this.packer.compile(stream);
|
||||||
|
|
||||||
|
return stream.Buffer;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user