From e8a40c45545e984312c2f5d8eec0d4195e009a38 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Mon, 27 Sep 2021 23:48:25 +0200 Subject: [PATCH] Deleted factory.ts file --- spec/factory.ts | 170 ------------------------------------------------ src/factory.ts | 99 ---------------------------- src/index.ts | 1 - 3 files changed, 270 deletions(-) delete mode 100644 spec/factory.ts delete mode 100644 src/factory.ts diff --git a/spec/factory.ts b/spec/factory.ts deleted file mode 100644 index 1d1bba9..0000000 --- a/spec/factory.ts +++ /dev/null @@ -1,170 +0,0 @@ -import { createPass } from "../lib/factory"; -import formatMessage, { ERROR } from "../lib/messages"; -import * as fs from "fs"; -import * as path from "path"; - -describe("createPass", () => { - it("should throw if first argument is not provided", async () => { - await expectAsync(createPass(undefined)).toBeRejectedWithError( - formatMessage(ERROR.CP_NO_OPTS), - ); - }); - - try { - let certificatesPath = "../certificates"; - - try { - fs.accessSync(path.resolve(__dirname, certificatesPath)); - } catch (err) { - certificatesPath = "../certs"; - - try { - fs.accessSync(path.resolve(__dirname, certificatesPath)); - } catch (err) { - certificatesPath = ""; - } - } - - if (certificatesPath) { - it("should return a pass instance", async () => { - await expectAsync( - createPass({ - model: path.resolve( - __dirname, - "../examples/models/exampleBooking.pass", - ), - certificates: { - signerCert: path.resolve( - __dirname, - certificatesPath, - "./signerCert.pem", - ), - signerKey: { - keyFile: path.resolve( - __dirname, - certificatesPath, - "./signerKey.pem", - ), - passphrase: "password1234", - }, - wwdr: path.resolve( - __dirname, - certificatesPath, - "./WWDR.pem", - ), - }, - }), - ).toBeResolved(); - }); - - describe("Model validation", () => { - it("Should reject with non valid model", async () => { - await expectAsync( - createPass({ - // @ts-expect-error - model: 0, - certificates: { - signerCert: path.resolve( - __dirname, - certificatesPath, - "./signerCert.pem", - ), - signerKey: { - keyFile: path.resolve( - __dirname, - certificatesPath, - "./signerKey.pem", - ), - passphrase: "password1234", - }, - wwdr: path.resolve( - __dirname, - certificatesPath, - "./WWDR.pem", - ), - }, - }), - ).toBeRejected(); - - await expectAsync( - createPass({ - model: undefined, - certificates: { - signerCert: path.resolve( - __dirname, - certificatesPath, - "./signerCert.pem", - ), - signerKey: { - keyFile: path.resolve( - __dirname, - certificatesPath, - "./signerKey.pem", - ), - passphrase: "password1234", - }, - wwdr: path.resolve( - __dirname, - certificatesPath, - "./WWDR.pem", - ), - }, - }), - ).toBeRejected(); - - await expectAsync( - createPass({ - model: null, - certificates: { - signerCert: path.resolve( - __dirname, - certificatesPath, - "./signerCert.pem", - ), - signerKey: { - keyFile: path.resolve( - __dirname, - certificatesPath, - "./signerKey.pem", - ), - passphrase: "password1234", - }, - wwdr: path.resolve( - __dirname, - certificatesPath, - "./WWDR.pem", - ), - }, - }), - ).toBeRejected(); - - await expectAsync( - createPass({ - model: {}, - certificates: { - signerCert: path.resolve( - __dirname, - certificatesPath, - "./signerCert.pem", - ), - signerKey: { - keyFile: path.resolve( - __dirname, - certificatesPath, - "./signerKey.pem", - ), - passphrase: "password1234", - }, - wwdr: path.resolve( - __dirname, - certificatesPath, - "./WWDR.pem", - ), - }, - }), - ).toBeRejected(); - }); - }); - } - } catch (err) {} -}); diff --git a/src/factory.ts b/src/factory.ts deleted file mode 100644 index e88c8aa..0000000 --- a/src/factory.ts +++ /dev/null @@ -1,99 +0,0 @@ -import { Pass } from "./pass"; -import * as Schemas from "./schemas"; -import formatMessage, { ERROR } from "./messages"; -import { getModelContents, readCertificatesFromOptions } from "./parser"; -import { splitBufferBundle } from "./utils"; -import { AbstractModel, AbstractFactoryOptions } from "./abstract"; - -/** - * Creates a new Pass instance. - * - * @param options Options to be used to create the instance or an Abstract Model reference - * @param additionalBuffers More buffers (with file name) to be added on runtime (if you are downloading some files from the web) - * @param abstractMissingData Additional data for abstract models, that might vary from pass to pass. - */ - -export async function createPass( - options: Schemas.FactoryOptions | InstanceType, - additionalBuffers?: Schemas.BundleUnit, - abstractMissingData?: Omit, -): Promise { - if ( - !( - options && - (options instanceof AbstractModel || Object.keys(options).length) - ) - ) { - throw new Error(formatMessage(ERROR.CP_NO_OPTS)); - } - - try { - if (options instanceof AbstractModel) { - let certificates: Schemas.CertificatesSchema; - let overrides: Schemas.OverridesSupportedOptions = { - ...(options.overrides || {}), - ...((abstractMissingData && abstractMissingData.overrides) || - {}), - }; - - if ( - !( - options.certificates && - options.certificates.signerCert && - options.certificates.signerKey - ) && - abstractMissingData.certificates - ) { - certificates = Object.assign( - options.certificates, - await readCertificatesFromOptions( - abstractMissingData.certificates, - ), - ); - } else { - certificates = options.certificates; - } - - return createPassInstance( - options.bundle, - certificates, - overrides, - additionalBuffers, - ); - } else { - const [bundle, certificates] = await Promise.all([ - getModelContents(options.model), - readCertificatesFromOptions(options.certificates), - ]); - - return createPassInstance( - bundle, - certificates, - options.overrides, - additionalBuffers, - ); - } - } catch (err) { - throw new Error(formatMessage(ERROR.CP_INIT, "pass", err)); - } -} - -function createPassInstance( - model: Schemas.PartitionedBundle, - certificates: Schemas.CertificatesSchema, - overrides: Schemas.OverridesSupportedOptions, - additionalBuffers?: Schemas.BundleUnit, -) { - if (additionalBuffers) { - const [additionalL10n, additionalBundle] = - splitBufferBundle(additionalBuffers); - Object.assign(model["l10nBundle"], additionalL10n); - Object.assign(model["bundle"], additionalBundle); - } - - return new Pass({ - model, - certificates, - overrides, - }); -} diff --git a/src/index.ts b/src/index.ts index 7d46253..a76a441 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ export type { Pass } from "./pass"; export type { AbstractModel } from "./abstract"; -export { createPass } from "./factory"; export { createAbstractModel } from "./abstract"; export { default as Bundle } from "./Bundle";