All files / src/server default-session.ts

94.28% Statements 33/35
100% Branches 0/0
85.71% Functions 12/14
94.11% Lines 32/34

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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96  1x 1x 1x     1x 1x     1x   1x 5x   5x   5x   5x   5x   5x 5x 5x 5x       1x               263x         80x 80x 80x 80x               93x 93x       5x 5x                       171x       1x       2x       80x       1x       4x      
import * as net from "net";
import * as util from "util";
import {Dictionary} from "../dictionary";
import {Logger} from "../logger";
import {IRespCommand} from "../resp/command/resp-command";
import {AbstractRedisToken} from "../resp/protocol/abstract-redis-token";
import {RespSerialize} from "../resp/protocol/resp-serialize";
import {ICmdReq, Session} from "./session";
 
// Tslint:disable-next-line
const resp = require("resp");
 
export class DefaultSession extends Session {
  protected logger: Logger = new Logger(module.id);
 
  private state: Dictionary<string, string> = new Dictionary();
 
  private name: string = "";
 
  private currentDb: number = 0;
 
  private lastCommand: string = "";
 
  constructor(private id: string, private socket: net.Socket) {
      super();
      delete this.commands;
      this.logger.debug(`constructor() - this.commands is ${this.commands}`);
  }
 
  public getAddress(): string {
      return util.format(
          "%s:%d",
          this.socket.remoteAddress,
          this.socket.remotePort
      );
  }
 
  public getId(): string {
      return this.id;
  }
 
  public publish(message: AbstractRedisToken<any>): void {
      // Send the message to the client
      this.logger.debug(`publish FROM: ${util.inspect(message)}`);
      const respResponse = new RespSerialize(message).serialize();
      this.logger.debug(`publish TO: ${respResponse}`);
      this.socket.write(Buffer.from(respResponse));
  }
 
  public close(): void {
      // Noop
  }
 
  public getValue(key: string) {
      this.logger.debug(`getValue(${key}: ${this.state.get(key)}`);
      return this.state.get(key);
  }
 
  public putValue(key: string, value: any): void {
      this.logger.debug(`putValue(${key}, ${value})`);
      this.state.put(
          key,
          value
      );
  }
 
  public removeValue(key: string) {
      this.logger.debug(`removeValue(${key})`);
      this.state.remove(key);
  }
 
  public getCurrentDb(): number {
      return this.currentDb;
  }
 
  public getLastCommand(): string {
      return this.lastCommand;
  }
 
  public setCurrentDb(db: number): void {
      this.currentDb = db;
  }
 
  public setLastCommand(cmd: string): void {
      this.lastCommand = cmd;
  }
 
  public setName(name: string): void {
      this.name = name;
  }
 
  public getName(): string {
      return this.name;
  }
}