diff --git a/src/PKPass.ts b/src/PKPass.ts index 1f9f820..8e91c3d 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -1,6 +1,7 @@ import FieldsArray from "../src/fieldsArray"; import { Certificates } from "../src/schemas"; import { default as Bundle, filesSymbol } from "./Bundle"; +import { getModelFolderContents } from "./parser"; const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol"); @@ -16,6 +17,47 @@ export class PKPass extends Bundle { public headerFields /******/ = new FieldsArray(this[fieldKeysPoolSymbol]); public backFields /********/ = new FieldsArray(this[fieldKeysPoolSymbol]); + /** + * Either create a pass from another one + * or a disk path. + * + * @param source + * @returns + */ + + static async from(source: PKPass | string): Promise { + let certificates: Certificates = undefined; + let buffers: NamedBuffers = undefined; + + if (source instanceof PKPass) { + /** Cloning is happening here */ + certificates = source.certificates; + buffers = {}; + + const buffersEntries = Object.entries(source[filesSymbol]); + + /** Cloning all the buffers to prevent unwanted edits */ + for (let i = 0; i < buffersEntries.length; i++) { + const [fileName, contentBuffer] = buffersEntries[i]; + + buffers[fileName] = Buffer.from(contentBuffer); + } + } else { + /** Disk model reading is happening here */ + + /** + * @TODO Rename bundles in something else. + * @TODO determine how to use localized files + */ + + const { bundle, l10nBundle } = await getModelFolderContents(source); + + buffers = bundle; + } + + return new PKPass(buffers, certificates); + } + constructor(buffers: NamedBuffers, certificates: Certificates) { super("application/vnd.apple.pkpass");