summaryrefslogtreecommitdiffstats
path: root/llparse-builder/src/utils.ts
blob: 3521b20e4bdb17dc35ca805d94f72408238730fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import * as assert from 'assert';
import { Buffer } from 'buffer';

/**
 * Internal
 */
export function toBuffer(value: number | string | Buffer): Buffer {
  let res: Buffer;
  if (Buffer.isBuffer(value)) {
    res = value;
  } else if (typeof value === 'string') {
    res = Buffer.from(value);
  } else {
    assert(0 <= value && value <= 0xff, 'Invalid byte value');
    res = Buffer.from([ value ]);
  }
  assert(res.length >= 1, 'Invalid key length');
  return res;
}