diff --git a/spec/Bundle.ts b/spec/Bundle.ts index 155fa73..e7ed8e7 100644 --- a/spec/Bundle.ts +++ b/spec/Bundle.ts @@ -85,6 +85,31 @@ describe("Bundle", () => { ).toThrowError(Error, "Cannot add file. Bundle is closed."); }); }); + + describe("getAsRaw", () => { + it("should freeze the bundle", () => { + bundle.getAsRaw(); + expect(bundle.isFrozen).toBe(true); + }); + + it("should return an object with filePath as key and Buffer as value", () => { + bundle.addBuffer("pass.json", Buffer.alloc(0)); + bundle.addBuffer("signature", Buffer.alloc(0)); + bundle.addBuffer("en.lproj/pass.strings", Buffer.alloc(0)); + bundle.addBuffer("en.lproj/icon.png", Buffer.alloc(0)); + + const list = bundle.getAsRaw(); + + expect(list["pass.json"]).not.toBeUndefined(); + expect(list["pass.json"]).toBeInstanceOf(Buffer); + expect(list["signature"]).not.toBeUndefined(); + expect(list["signature"]).toBeInstanceOf(Buffer); + expect(list["en.lproj/pass.strings"]).not.toBeUndefined(); + expect(list["en.lproj/pass.strings"]).toBeInstanceOf(Buffer); + expect(list["en.lproj/icon.png"]).not.toBeUndefined(); + expect(list["en.lproj/icon.png"]).toBeInstanceOf(Buffer); + }); + }); }); }); diff --git a/src/Bundle.ts b/src/Bundle.ts index d9d7481..1d3783f 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -127,4 +127,19 @@ export default class Bundle { this[freezeSymbol](); return this[archiveSymbol].outputStream; } + + /** + * Closes the bundle and returns it as an object. + * This allows developers to choose a different way + * of serving, analyzing or zipping the file, outside the + * default compression system. + * + * @returns a frozen object containing files paths as key + * and Buffers as content. + */ + + public getAsRaw(): { [filePath: string]: Buffer } { + this[freezeSymbol](); + return Object.freeze({ ...this[filesSymbol] }); + } } diff --git a/src/PKPass.ts b/src/PKPass.ts index 814efe8..3c42601 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -630,6 +630,28 @@ export default class PKPass extends Bundle { return super.getAsStream(); } + /** + * Exports the pass as a list of file paths and buffers. + * When this method is invoked, the bundle will get + * frozen and, thus, no files will be allowed to be + * added any further. + * + * This allows developers to choose a different way + * of serving, analyzing or zipping the file, outside the + * default compression system. + * + * @returns a frozen object containing files paths as key + * and Buffers as content. + */ + + public getAsRaw(): { [filePath: string]: Buffer } { + if (!this.isFrozen) { + this[closePassSymbol](); + } + + return super.getAsRaw(); + } + // ************************** // // *** DATA SETUP METHODS *** // // ************************** //