Add buffer packer
This commit is contained in:
@ -2,3 +2,4 @@ export * from "./packer/local";
|
||||
export * from "./packer/express";
|
||||
export * from "./packer/packer";
|
||||
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);
|
||||
}
|
||||
}
|
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