All files idempotency.js

100% Statements 25/25
100% Branches 8/8
100% Functions 3/3
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 269x 9x 9x 9x 9x 9x 9x 9x 10176x 10176x 10176x 10176x 10176x 9x 9x 31x 31x 9x 9x 9x 27x 10x 10x 17x 27x  
// Idempotency keys (DESIGN 2.2).
import { localError } from './errors.js';
import { randomBytes, toHex } from './util.js';
 
const KEY_PATTERN = /^[\x21-\x7E]{1,200}$/;
 
/** 32 lowercase hex characters: a UUID v4 without dashes. */
export function generateIdempotencyKey() {
  const b = randomBytes(16);
  b[6] = (b[6] & 0x0f) | 0x40;
  b[8] = (b[8] & 0x3f) | 0x80;
  return toHex(b);
}
 
export function isValidIdempotencyKey(key) {
  return typeof key === 'string' && KEY_PATTERN.test(key);
}
 
/** Throws InvalidParameterException 90010 unless key matches ^[\x21-\x7E]{1,200}$. */
export function validateIdempotencyKey(key) {
  if (!isValidIdempotencyKey(key)) {
    throw localError(90010, 'idempotencyKey must be 1 to 200 visible ASCII characters (^[\\x21-\\x7E]{1,200}$)');
  }
  return key;
}