Added Bundle.prototype.files getter to prevent exposing filesSymbol

This commit is contained in:
Alexander Cerutti
2021-09-26 18:21:25 +02:00
parent 1b3647da8f
commit 6f7a597cbe
3 changed files with 15 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
import { Stream } from "stream"; import { Stream } from "stream";
import { default as Bundle, filesSymbol } from "../lib/Bundle"; import { default as Bundle } from "../lib/Bundle";
describe("Bundle", () => { describe("Bundle", () => {
let bundle: InstanceType<typeof Bundle>; let bundle: InstanceType<typeof Bundle>;
@@ -24,7 +24,7 @@ describe("Bundle", () => {
const buffer = Buffer.alloc(0); const buffer = Buffer.alloc(0);
bundle.addBuffer("pass.json", buffer); bundle.addBuffer("pass.json", buffer);
expect(bundle[filesSymbol]).toEqual({ "pass.json": buffer }); expect(bundle.files).toEqual({ "pass.json": buffer });
}); });
it("should throw error if freezed", async () => { it("should throw error if freezed", async () => {

View File

@@ -1,7 +1,7 @@
import { Stream } from "stream"; import { Stream } from "stream";
import { ZipFile } from "yazl"; import { ZipFile } from "yazl";
export const filesSymbol = Symbol("bundleFiles"); const filesSymbol = Symbol("bundleFiles");
const bundleStateSymbol = Symbol("state"); const bundleStateSymbol = Symbol("state");
const archiveSymbol = Symbol("zip"); const archiveSymbol = Symbol("zip");
@@ -78,6 +78,14 @@ export default class Bundle {
return this[bundleStateSymbol] === BundleState.CLOSED; return this[bundleStateSymbol] === BundleState.CLOSED;
} }
/**
* Returns the list of files added to the bundle
*/
public get files() {
return this[filesSymbol];
}
/** /**
* Allows files to be added to the bundle. * Allows files to be added to the bundle.
* If the bundle is closed, it will throw an error. * If the bundle is closed, it will throw an error.

View File

@@ -1,5 +1,5 @@
import FieldsArray from "./fieldsArray"; import FieldsArray from "./fieldsArray";
import { default as Bundle, filesSymbol } from "./Bundle"; import { default as Bundle } from "./Bundle";
import { getModelFolderContents } from "./parser"; import { getModelFolderContents } from "./parser";
import * as Schemas from "./schemas"; import * as Schemas from "./schemas";
import { Stream } from "stream"; import { Stream } from "stream";
@@ -73,7 +73,7 @@ export default class PKPass extends Bundle {
certificates = source.certificates; certificates = source.certificates;
buffers = {}; buffers = {};
const buffersEntries = Object.entries(source[filesSymbol]); const buffersEntries = Object.entries(source.files);
/** Cloning all the buffers to prevent unwanted edits */ /** Cloning all the buffers to prevent unwanted edits */
for (let i = 0; i < buffersEntries.length; i++) { for (let i = 0; i < buffersEntries.length; i++) {
@@ -285,7 +285,7 @@ export default class PKPass extends Bundle {
} }
if (/pass\.json/.test(pathName)) { if (/pass\.json/.test(pathName)) {
if (this[filesSymbol]["pass.json"]) { if (this.files["pass.json"]) {
/** /**
* Ignoring any further addition. In a * Ignoring any further addition. In a
* future we might consider merging instead * future we might consider merging instead
@@ -374,7 +374,7 @@ export default class PKPass extends Bundle {
} }
private [createManifestSymbol]() { private [createManifestSymbol]() {
return Object.entries(this[filesSymbol]).reduce<Schemas.Manifest>( return Object.entries(this.files).reduce<Schemas.Manifest>(
(acc, [fileName, buffer]) => { (acc, [fileName, buffer]) => {
const hashFlow = forge.md.sha1.create(); const hashFlow = forge.md.sha1.create();