Added first untested and incomplete implementation of PKPass.from

This commit is contained in:
Alexander Cerutti
2021-09-18 22:07:13 +02:00
parent a970232623
commit a38c235cf8

View File

@@ -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<PKPass> {
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");