Skip to content

Commit

Permalink
feat: lazy construction
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWilbur committed Aug 30, 2023
1 parent 0f90b84 commit 2d68c1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 8 additions & 2 deletions source/codecs/ber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ class BERElement extends X690Element {
}

set sequence (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequence (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand All @@ -218,14 +221,17 @@ class BERElement extends X690Element {
}

set sequenceOf (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequenceOf (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand Down
10 changes: 8 additions & 2 deletions source/codecs/cer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,17 @@ class CERElement extends X690Element {
}

set sequence (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequence (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand All @@ -239,14 +242,17 @@ class CERElement extends X690Element {
}

set sequenceOf (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequenceOf (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand Down
10 changes: 8 additions & 2 deletions source/codecs/der.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,17 @@ class DERElement extends X690Element {
}

set sequence (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequence (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand All @@ -200,14 +203,17 @@ class DERElement extends X690Element {
}

set sequenceOf (value: SEQUENCE<ASN1Element>) {
this.value = encodeSequence(value);
this.construct(value);
this.construction = ASN1Construction.constructed;
}

get sequenceOf (): SEQUENCE<ASN1Element> {
if (this.construction !== ASN1Construction.constructed) {
throw new errors.ASN1ConstructionError("SET or SEQUENCE cannot be primitively constructed.", this);
}
if (Array.isArray(this._value)) {
return this._value;
}
return decodeSequence(this.value);
}

Expand Down

0 comments on commit 2d68c1f

Please sign in to comment.