Skip to main content

Function: splitPemBlocks()

splitPemBlocks(text): string[]

Splits a text that may contain several concatenated PEM blocks into one string per block. Text without any -----BEGIN marker (e.g. a raw base64 SPKI key) is returned as a single entry. Whitespace-only input yields an empty array.

Each block runs from a well-formed -----BEGIN <label>----- marker through the next well-formed -----END <label>----- marker (inclusive); text outside blocks is ignored and order is preserved. Implemented as a single forward scan with indexOf rather than a regular expression, because the input is untrusted (key material from config/env/files) and a backtracking regex over many repeated -----BEGIN prefixes without a matching -----END runs in polynomial time (CodeQL js/polynomial-redos).

Parameters​

text​

string

Returns​

string[]