From ef0c28b744da326d9becb5adad14ecf7ed9d81c6 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 18 Sep 2021 23:18:12 +0200 Subject: [PATCH] Added untested implementation of PKPass.pack method --- src/PKPass.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/PKPass.ts b/src/PKPass.ts index c5cfb36..cf0e2fd 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -66,6 +66,35 @@ export class PKPass extends Bundle { return new PKPass(buffers, certificates); } + /** + * + * @param passes + */ + + static async pack(...passes: PKPass[]): Promise { + if (!passes.every((pass) => pass instanceof PKPass)) { + throw new Error( + "Cannot pack passes. Only PKPass instances allowed", + ); + } + + const bundle = Bundle.autoFreezable("application/vnd.apple.pkpasses"); + + const buffers = await Promise.all( + passes.map((pass) => pass.getAsBuffer()), + ); + + for (let i = 0; i < buffers.length; i++) { + bundle.addBuffer(`packed-pass-${i + 1}.pkpass`, buffers[i]); + } + + return bundle; + } + + // **************** // + // *** INSTANCE *** // + // **************** // + constructor(buffers: NamedBuffers, certificates: Certificates) { super("application/vnd.apple.pkpass");