• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeculiarVentures / ASN1.js / 14130271268

28 Mar 2025 01:41PM UTC coverage: 77.767% (-0.03%) from 77.8%
14130271268

push

github

web-flow
Merge pull request #114 from PeculiarVentures/update

Update dependencies and migrate testing framework to Jest

386 of 652 branches covered (59.2%)

102 of 113 new or added lines in 55 files covered. (90.27%)

16 existing lines in 7 files now uncovered.

1581 of 2033 relevant lines covered (77.77%)

5777.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.16
/src/BaseBlock.ts
1
import * as pvtsutils from "pvtsutils";
6✔
2
import * as pvutils from "pvutils";
6✔
3
import { IBerConvertible } from "./types";
4
import {
6✔
5
  LocalBaseBlockJson, LocalBaseBlockParams, LocalBaseBlock,
6
} from "./internals/LocalBaseBlock";
7
import {
6✔
8
  LocalIdentificationBlock, LocalIdentificationBlockJson, LocalIdentificationBlockParams,
9
} from "./internals/LocalIdentificationBlock";
10
import {
6✔
11
  LocalLengthBlock, LocalLengthBlockJson, LocalLengthBlockParams,
12
} from "./internals/LocalLengthBlock";
13
import { ViewWriter } from "./ViewWriter";
6✔
14
import { ValueBlock, ValueBlockJson } from "./ValueBlock";
6✔
15
import { EMPTY_BUFFER, EMPTY_STRING } from "./internals/constants";
6✔
16
import { typeStore } from "./TypeStore";
6✔
17

18
export interface IBaseBlock {
19
  name: string;
20
  optional: boolean;
21
  primitiveSchema?: BaseBlock;
22
}
23

24
export interface BaseBlockParams extends
25
  LocalBaseBlockParams, LocalIdentificationBlockParams, LocalLengthBlockParams, Partial<IBaseBlock> { }
26

27
export type ValueBlockConstructor<T extends ValueBlock = ValueBlock> = new (...args: any[]) => T;
28

29
// eslint-disable-next-line @stylistic/max-len
30
export interface BaseBlockJson<T extends LocalBaseBlockJson = LocalBaseBlockJson> extends LocalBaseBlockJson, Omit<IBaseBlock, "primitiveSchema"> {
31
  idBlock: LocalIdentificationBlockJson;
32
  lenBlock: LocalLengthBlockJson;
33
  valueBlock: T;
34
  primitiveSchema?: BaseBlockJson;
35
}
36

37
export type StringEncoding = "ascii" | "hex";
38

39
// eslint-disable-next-line @stylistic/max-len
40
export class BaseBlock<T extends ValueBlock = ValueBlock, J extends ValueBlockJson = ValueBlockJson> extends LocalBaseBlock implements IBaseBlock, IBerConvertible {
6✔
41
  public static override NAME = "BaseBlock";
6✔
42

43
  public idBlock: LocalIdentificationBlock;
44
  public lenBlock: LocalLengthBlock;
45
  public valueBlock: T;
46
  public name: string;
47
  public optional: boolean;
48
  public primitiveSchema?: BaseBlock;
49

50
  constructor({
10✔
51
    name = EMPTY_STRING,
764✔
52
    optional = false,
764✔
53
    primitiveSchema,
54
    ...parameters
55
  }: BaseBlockParams = {}, valueBlockType?: ValueBlockConstructor<T>) {
56
    super(parameters);
770✔
57

58
    this.name = name;
770✔
59
    this.optional = optional;
770✔
60
    if (primitiveSchema) {
770✔
61
      this.primitiveSchema = primitiveSchema;
4✔
62
    }
63

64
    this.idBlock = new LocalIdentificationBlock(parameters);
770✔
65
    this.lenBlock = new LocalLengthBlock(parameters);
770✔
66
    this.valueBlock = valueBlockType ? new valueBlockType(parameters) : new ValueBlock(parameters) as unknown as T;
770✔
67
  }
68

69
  public fromBER(inputBuffer: Uint8Array, inputOffset: number, inputLength: number): number {
70
    const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm)
210✔
71
      ? inputLength
72
      : this.lenBlock.length);
73
    if (resultOffset === -1) {
210✔
74
      this.error = this.valueBlock.error;
48✔
75

76
      return resultOffset;
48✔
77
    }
78

79
    if (!this.idBlock.error.length)
162✔
80
      this.blockLength += this.idBlock.blockLength;
162✔
81

82
    if (!this.lenBlock.error.length)
162✔
83
      this.blockLength += this.lenBlock.blockLength;
162✔
84

85
    if (!this.valueBlock.error.length)
162✔
86
      this.blockLength += this.valueBlock.blockLength;
162✔
87

88
    return resultOffset;
162✔
89
  }
90

91
  public toBER(sizeOnly?: boolean, writer?: ViewWriter): ArrayBuffer {
92
    const _writer = writer || new ViewWriter();
116✔
93

94
    if (!writer) {
116✔
95
      prepareIndefiniteForm(this);
100✔
96
    }
97

98
    const idBlockBuf = this.idBlock.toBER(sizeOnly);
116✔
99

100
    _writer.write(idBlockBuf);
116✔
101

102
    if (this.lenBlock.isIndefiniteForm) {
116✔
103
      _writer.write(new Uint8Array([0x80]).buffer);
4✔
104

105
      this.valueBlock.toBER(sizeOnly, _writer);
4✔
106

107
      _writer.write(new ArrayBuffer(2));
4✔
108
    } else {
109
      const valueBlockBuf = this.valueBlock.toBER(sizeOnly);
112✔
110
      this.lenBlock.length = valueBlockBuf.byteLength;
112✔
111
      const lenBlockBuf = this.lenBlock.toBER(sizeOnly);
112✔
112

113
      _writer.write(lenBlockBuf);
112✔
114
      _writer.write(valueBlockBuf);
112✔
115
    }
116

117
    if (!writer) {
116✔
118
      return _writer.final();
100✔
119
    }
120

121
    return EMPTY_BUFFER;
16✔
122
  }
123

124
  public override toJSON(): BaseBlockJson<J> {
125
    const object: BaseBlockJson = {
12✔
126
      ...super.toJSON(),
127
      idBlock: this.idBlock.toJSON(),
128
      lenBlock: this.lenBlock.toJSON(),
129
      valueBlock: this.valueBlock.toJSON(),
130
      name: this.name,
131
      optional: this.optional,
132
    };
133

134
    if (this.primitiveSchema)
12✔
135
      object.primitiveSchema = this.primitiveSchema.toJSON();
2✔
136

137
    return object as BaseBlockJson<J>;
12✔
138
  }
139

140
  public override toString(encoding: StringEncoding = "ascii"): string {
32✔
141
    if (encoding === "ascii") {
246✔
142
      return this.onAsciiEncoding();
158✔
143
    }
144

145
    return pvtsutils.Convert.ToHex(this.toBER());
88✔
146
  }
147

148
  protected onAsciiEncoding(): string {
NEW
149
    const name = (this.constructor as typeof BaseBlock).NAME;
×
NEW
150
    const value = pvtsutils.Convert.ToHex(this.valueBlock.valueBeforeDecodeView);
×
151

NEW
152
    return `${name} : ${value}`;
×
153
  }
154

155
  /**
156
   * Determines whether two object instances are equal
157
   * @param other Object to compare with the current object
158
   */
159
  public isEqual(other: unknown): other is this {
160
    if (this === other) {
×
161
      return true;
×
162
    }
163

164
    // Check input type
165
    if (!(other instanceof this.constructor)) {
×
166
      return false;
×
167
    }
168

169
    const thisRaw = this.toBER();
×
170
    const otherRaw = (other as BaseBlock).toBER();
×
171

172
    return pvutils.isEqualBuffer(thisRaw, otherRaw);
×
173
  }
174
}
175

176
/**
177
 * Recursive function which checks and enables isIndefiniteForm flag for constructed blocks
178
 * if any child has that flag enabled
179
 * @param baseBlock Base ASN.1 block
180
 * @returns Returns `true` if incoming block is `indefinite form`
181
 */
182
function prepareIndefiniteForm(baseBlock: BaseBlock): boolean {
183
  if (baseBlock instanceof typeStore.Constructed) {
100!
184
    for (const value of baseBlock.valueBlock.value) {
×
185
      if (prepareIndefiniteForm(value)) {
×
186
        baseBlock.lenBlock.isIndefiniteForm = true;
×
187
      }
188
    }
189
  }
190

191
  return !!baseBlock.lenBlock?.isIndefiniteForm;
100!
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc