All files / core/lib/Interfaces DSN.ts

100% Statements 16/16
90% Branches 9/10
100% Functions 4/4
100% Lines 16/16
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  2x 2x 2x 2x   22x 22x 17x   2x 4x     2x 22x 22x 17x                     5x     2x   2x                              
import { SentryError } from '../Sentry';
 
interface IDSNParts {
  source: string;
  protocol: string;
  user: string;
  pass: string;
  host: string;
  port: string;
  path: string;
}
 
const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(:\w+)?@)([\w\.-]+)(?::(\d+))?(\/.*)/;
 
export class DSN {
  private dsnString: string;
  private dsn: IDSNParts;
 
  constructor(dsnString: string) {
    this.dsnString = dsnString;
    this.parseDsn();
    return this;
  }
 
  public getDsn(withPass: boolean) {
    return (
      `${this.dsn.protocol}://${this.dsn.user}${withPass ? this.dsn.pass : ''}` +
      `@${this.dsn.host}${this.dsn.port ? ':' + this.dsn.port : ''}${this.dsn.path}`
    );
  }
 
  private parseDsn() {
    const match = DSN_REGEX.exec(this.dsnString);
    if (match) {
      this.dsn = {
        source: match[0],
        protocol: match[1],
        user: match[2],
        pass: match[3] || '',
        host: match[4],
        port: match[5] || '',
        path: match[6],
      };
    } else {
      throw new SentryError('invalid dsn');
    }
  }
}