Skip to content

Core API

dweller long gone edited this page May 21, 2026 · 1 revision

Core API

The Core API lives in app/static/pkistudio-core.js. It is available from the npm package entry point and from @pkistudio/pkistudiojs/core.

const pkistudio = require('@pkistudio/pkistudiojs');

const document = pkistudio.parseInput(
  new Uint8Array([0x30, 0x03, 0x02, 0x01, 0x01])
);

console.log(pkistudio.serializeTree(document.nodes));

The same file can be loaded directly in a browser. It exposes window.PkiStudioCore.

<script src="pkistudio-core.js"></script>
<script>
  const summary = window.PkiStudioCore.parseAsn1('3003020101');
  console.log(summary.nodes[0].tagName);
</script>

Main Parsing APIs

parseInput(input, options)

Parses DER/BER bytes, PEM text, headerless base64 text, or HEX text.

Returns:

  • format: detected or requested input format.
  • bytes: decoded input bytes.
  • encodedBytes: re-encoded bytes.
  • nodes: parsed ASN.1 node tree.

By default, parsing validates that re-encoding matches the decoded bytes. Pass validateRoundTrip: false to skip that check.

parseAsn1(input, options)

Returns a JSON-friendly parsed summary:

  • format
  • length
  • nodes

This is useful when callers do not need the internal node objects.

Input Format Options

Use options.format when input should not be auto-detected.

Supported values:

  • auto
  • der
  • ber
  • pem
  • base64
  • headerless-pem
  • hex

Serialization APIs

serializeTree(nodes, options)

Converts a parsed node tree into plain JavaScript objects.

serializeNode(node, options)

Serializes a single parsed node.

Serialization options include:

  • maxDepth: limit serialized child depth.
  • includeRawValue: include raw value bytes as lowercase hex.
  • includeHexPreview: include compact hex previews for primitive values.
  • hexPreviewLength: control preview length.
  • oidNames: object keyed by dotted OID strings.
  • oidResolver: resolver object with a resolve(oid) method.

Serialized node fields include tag name, tag class, tag number, constructed flag, byte offsets, length metadata, indefinite-length status, encapsulation status, decoded value, optional OID name, optional hex preview, and children.

Encoding APIs

  • encodeNodes(nodes): re-encode a node list.
  • encodeNode(node): re-encode a single parsed node.
  • getNodeBytes(nodes, nodeId): re-encode a parsed node and its subtree by node ID.

Helper APIs

The Core API also exports lower-level helpers:

  • decodePem(text)
  • hexToBytes(text)
  • base64ToBytes(base64)
  • bytesToBase64(bytes)
  • decodeOid(bytes)
  • encodeOid(text)
  • getTagName(node)
  • describeValue(node)
  • findNodeById(nodes, id)
  • flattenNodes(nodes)
  • resolveOid(oid, oidNamesOrResolver)

It also exports constants and utilities such as VERSION, CLASS_NAMES, UNIVERSAL_TAGS, concatBytes, bytesEqual, decodeInput, parseElements, getNodeValueBytes, isPemText, and toLowerHexString.

Minimal Example

const core = require('@pkistudio/pkistudiojs/core');

const document = core.parseInput('3003020101');
const root = document.nodes[0];

console.log(document.format); // HEX
console.log(core.getTagName(root)); // SEQUENCE
console.log(core.serializeNode(root));
console.log(Array.from(core.getNodeBytes(document.nodes, root.id)));

Clone this wiki locally