index.js
function decode(pem) {
  if (Buffer.isBuffer(pem)) {
    pem = pem.toString('ascii')
  }

  const lines = pem.trim().split('\n')

  if (lines.length < 3) {
    throw new Error('Invalid PEM data.')
  }

  const match = header.exec(lines[0])

  if (match === null) {
    throw new Error('Invalid label.')
  }

  const label = match[1]
  let i = 1

  for (; i < lines.length; ++i) {
    if (lines[i].startsWith(after)) {
      break
    }
  }

  const footer = new RegExp(`^${after} ${label}${endline}\r?\n?$`)

  if (footer.exec(lines[i]) === null) {
    throw new Error('Invalid end of file.')
  }

  const body = lines.slice(1, i).join('\n').replace(/\r?\n/g, '')
  return Buffer.from(body, 'base64')
}
function decode(pem) {
  if (Buffer.isBuffer(pem)) {
    pem = pem.toString('ascii')
  }

  const lines = pem.trim().split('\n')

  if (lines.length < 3) {
    throw new Error('Invalid PEM data.')
  }

  const match = header.exec(lines[0])

  if (match === null) {
    throw new Error('Invalid label.')
  }

  const label = match[1]
  let i = 1

  for (; i < lines.length; ++i) {
    if (lines[i].startsWith(after)) {
      break
    }
  }

  const footer = new RegExp(`^${after} ${label}${endline}\r?\n?$`)

  if (footer.exec(lines[i]) === null) {
    throw new Error('Invalid end of file.')
  }

  const body = lines.slice(1, i).join('\n').replace(/\r?\n/g, '')
  return Buffer.from(body, 'base64')
}
function decode ( pem ) { if ( Buffer . isBuffer ( pem ) ) { pem = pem . toString ( 'ascii' ) } const lines = pem . trim ( ) . split ( '\n' ) if ( lines . length < 3 ) { throw new Error ( 'Invalid PEM data.' ) } const match = header . exec ( lines [ 0 ] ) if ( match === null ) { throw new Error ( 'Invalid label.' ) } const label = match [ 1 ] let i = 1 for ( ; i < lines . length ; ++ i ) { if ( lines [ i ] . startsWith ( after ) ) { break } } const footer = new RegExp ( ` ${ after } ${ label } ${ endline } \r \n ` ) if ( footer . exec ( lines [ i ] ) === null ) { throw new Error ( 'Invalid end of file.' ) } const body = lines . slice ( 1 , i ) . join ( '\n' ) . replace ( / \r?\n / g , '' ) return Buffer . from ( body , 'base64' ) }
Convert PEM formatted data to raw buffer.
@param {Buffer|String} pem
@returns {Buffer}
Convert PEM formatted data to raw buffer .