Skip to content

Commit

Permalink
fix: implement Registry class for managing circular dependencies and …
Browse files Browse the repository at this point in the history
…enhance CMS imports
  • Loading branch information
microshine committed Jan 7, 2025
1 parent fa970b6 commit c5cd79b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
34 changes: 34 additions & 0 deletions packages/doc/src/Registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CRL } from "./cms/CRL";
import type { OCSP } from "./cms/OCSP";

/**
* Registry class is designed for internal usage to resolve circular dependencies.
* Implements Singleton pattern to maintain a global registry of key-value pairs.
* @internal
*/
export class Registry {
private static instance: Registry;
private items = new Map<string, unknown>();

public static getInstance(): Registry {
if (!Registry.instance) {
Registry.instance = new Registry();
}
return Registry.instance;
}

public register(key: string, value: unknown): void {
this.items.set(key, value);
}

public get(key: "CRL"): typeof CRL;
public get(key: "OCSP"): typeof OCSP;
public get(key: string): unknown;
public get(key: string): unknown {
const item = this.items.get(key);
if (!item) {
throw new Error(`Item '${key}' is not registered in Registry`);
}
return item;
}
}
3 changes: 2 additions & 1 deletion packages/doc/src/cms/AdobeRevocationInfoArchival.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
AsnTypeTypes
} from "@peculiar/asn1-schema";
import * as asn1js from "asn1js";
import { CmsAttribute, CmsAttributeFactory } from "./attributes";
import { CmsAttribute } from "./attributes/Attribute";
import { CmsAttributeFactory } from "./attributes/AttributeFactory";
import { CRL } from "./CRL";
import { OCSP } from "./OCSP";

Expand Down
11 changes: 7 additions & 4 deletions packages/doc/src/cms/DefaultCertificateStorageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ export class DefaultCertificateStorageHandler
try {
const raw = await this.requestCRL(uri);

const crl = Registry.getInstance().get("CRL");
return {
result: CRL.fromBER(raw),
result: crl.fromBER(raw),
target: this
};
} catch (e) {
Expand Down Expand Up @@ -462,8 +463,9 @@ export class DefaultCertificateStorageHandler
};
}

const ocsp = Registry.getInstance().get("OCSP");
return {
result: OCSP.fromBER(ocspResp.responseBytes!.response),
result: ocsp.fromBER(ocspResp.responseBytes!.response),
target: this
};
}
Expand Down Expand Up @@ -539,8 +541,8 @@ export class DefaultCertificateStorageHandler
}
}

import { CRL } from "./CRL";
import { OCSP } from "./OCSP";
import type { CRL } from "./CRL";
import type { OCSP } from "./OCSP";
import { CertificateID } from "./CertID";
import { PKIUtils } from "./PKIUtils";

Expand All @@ -550,3 +552,4 @@ import type {
IsTrustedResult,
RevocationType
} from "./ICertificateStorageHandler";
import { Registry } from "../Registry";
21 changes: 10 additions & 11 deletions packages/doc/src/cms/OCSP.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import * as asnOcsp from "@peculiar/asn1-ocsp";
import {
AlgorithmIdentifier,
CRLReason,
CRLReasons,
Name
} from "@peculiar/asn1-x509";
import * as asnSchema from "@peculiar/asn1-schema";
import * as x509 from "@peculiar/x509";
import * as asn1js from "asn1js";
import * as pkijs from "pkijs";
import { BufferSource, BufferSourceConverter } from "pvtsutils";

import {
ICertificateStorage,
ICertificateStorageHandler
} from "./ICertificateStorageHandler";
import { AsnEncoded } from "./AsnEncoded";

export interface OCSPResponse {
Expand Down Expand Up @@ -228,14 +237,4 @@ export class OCSP
import { AlgorithmFactory } from "./AlgorithmFactory";
import { PKIUtils } from "./PKIUtils";
import { CertificateID } from "./CertID";
import {
AlgorithmIdentifier,
CRLReason,
CRLReasons,
Name
} from "@peculiar/asn1-x509";
import {
ICertificateStorage,
ICertificateStorageHandler
} from "./ICertificateStorageHandler";
import { OCSPCertificateStorageHandler } from "./OCSPCertificateStorageHandler";
15 changes: 7 additions & 8 deletions packages/doc/src/cms/SignerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export enum CMSAttributeTypes {
export class CMSSignerInfo extends AsnEncoded<pkijs.SignerInfo> {
public parent: CMSSignedData | null = null;

public signedAttributes: ReadonlyArray<attributes.CmsAttribute> = [];
public unsignedAttributes: attributes.CmsAttribute[] = [];
public signedAttributes: ReadonlyArray<CmsAttribute> = [];
public unsignedAttributes: CmsAttribute[] = [];

constructor() {
super();
Expand Down Expand Up @@ -111,13 +111,11 @@ export class CMSSignerInfo extends AsnEncoded<pkijs.SignerInfo> {
return result;
}

protected readAttributes(
attrs?: pkijs.Attribute[]
): attributes.CmsAttribute[] {
const res: attributes.CmsAttribute[] = [];
protected readAttributes(attrs?: pkijs.Attribute[]): CmsAttribute[] {
const res: CmsAttribute[] = [];
if (attrs) {
for (const attr of attrs) {
const attrConst = attributes.CmsAttributeFactory.get(attr.type);
const attrConst = CmsAttributeFactory.get(attr.type);
const cmsAttr = new attrConst();
cmsAttr.fromBER(attr.toSchema().toBER());

Expand Down Expand Up @@ -204,7 +202,8 @@ export class CMSSignerInfo extends AsnEncoded<pkijs.SignerInfo> {
}
}

import * as attributes from "./attributes";
import { CmsAttribute } from "./attributes/Attribute";
import { CmsAttributeFactory } from "./attributes/AttributeFactory";
import { AlgorithmFactory } from "./AlgorithmFactory";

import type { CMSSignedData } from "./SignedData";
1 change: 1 addition & 0 deletions packages/doc/src/cms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from "./SignedData";
export * from "./SignerInfo";
export * from "./TimeStampToken";
export * from "./CRL";
export * from "./OCSPCertificateStorageHandler";
export * from "./OCSP";
export * from "./PKIUtils";
export * from "./AdobeRevocationInfoArchival";
Expand Down
8 changes: 8 additions & 0 deletions packages/doc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export * from "./Dss";
export * from "./CertificateStorageHandler";
export * from "./embedded_file";
export * from "./forms";

import { Registry } from "./Registry";
import { CRL } from "./cms/CRL";
import { OCSP } from "./cms/OCSP";

const registry = Registry.getInstance();
registry.register("OCSP", OCSP);
registry.register("CRL", CRL);

0 comments on commit c5cd79b

Please sign in to comment.