All files / json-pack/src/rpc RpcMessageEncoder.ts

99.11% Statements 112/113
95.23% Branches 20/21
100% Functions 10/10
100% Lines 107/107

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 19232x 32x 32x 32x 32x                 32x 629x                     26x 26x                   9x 9x                 4x 4x       1022x 1022x       1022x 1011x 11x 7x 4x 4x                         1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 1823x 18x 1805x 2x 2x                     779x 779x 779x 779x 779x 779x 779x 779x 779x 779x 779x 779x 779x 779x 2x 2x   779x 5x 4x   1x 1x                     14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 8x 8x 8x 8x   14x 6x 6x   14x       4425x 4425x 4425x 4425x 4425x 4425x 4425x 4425x 4425x 4425x 4425x 4425x 37x 37x 37x 37x 37x 15x 15x     4425x      
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {RpcMsgType, RpcReplyStat, RPC_VERSION} from './constants';
import {RpcEncodingError} from './errors';
import {
  type RpcOpaqueAuth,
  RpcCallMessage,
  RpcAcceptedReplyMessage,
  RpcRejectedReplyMessage,
  type RpcMessage,
} from './messages';
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers';
 
export class RpcMessageEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> {
  constructor(public readonly writer: W = new Writer() as any) {}
 
  public encodeCall(
    xid: number,
    prog: number,
    vers: number,
    proc: number,
    cred: RpcOpaqueAuth,
    verf: RpcOpaqueAuth,
    params?: Reader | Uint8Array,
  ): Uint8Array {
    this.writeCall(xid, prog, vers, proc, cred, verf, params);
    return this.writer.flush();
  }
 
  public encodeAcceptedReply(
    xid: number,
    verf: RpcOpaqueAuth,
    acceptStat: number,
    mismatchInfo?: {low: number; high: number},
    results?: Reader | Uint8Array,
  ): Uint8Array {
    this.writeAcceptedReply(xid, verf, acceptStat, mismatchInfo, results);
    return this.writer.flush();
  }
 
  public encodeRejectedReply(
    xid: number,
    rejectStat: number,
    mismatchInfo?: {low: number; high: number},
    authStat?: number,
  ): Uint8Array {
    this.writeRejectedReply(xid, rejectStat, mismatchInfo, authStat);
    return this.writer.flush();
  }
 
  public encodeMessage(msg: RpcMessage): Uint8Array {
    this.writeMessage(msg);
    return this.writer.flush();
  }
 
  public writeMessage(msg: RpcMessage): void {
    if (msg instanceof RpcCallMessage) {
      this.writeCall(msg.xid, msg.prog, msg.vers, msg.proc, msg.cred, msg.verf, msg.params);
    } else if (msg instanceof RpcAcceptedReplyMessage) {
      this.writeAcceptedReply(msg.xid, msg.verf, msg.stat, msg.mismatchInfo, msg.results);
    } else if (msg instanceof RpcRejectedReplyMessage) {
      this.writeRejectedReply(msg.xid, msg.stat, msg.mismatchInfo, msg.authStat);
    }
  }
 
  public writeCall(
    xid: number,
    prog: number,
    vers: number,
    proc: number,
    cred: RpcOpaqueAuth,
    verf: RpcOpaqueAuth,
    params?: Reader | Uint8Array,
  ): void {
    const writer = this.writer;
    writer.ensureCapacity(16 * 4);
    const view = writer.view;
    let x = writer.x;
    view.setUint32(x, xid, false);
    x += 4;
    view.setUint32(x, RpcMsgType.CALL, false);
    x += 4;
    view.setUint32(x, RPC_VERSION, false);
    x += 4;
    view.setUint32(x, prog, false);
    x += 4;
    view.setUint32(x, vers, false);
    x += 4;
    view.setUint32(x, proc, false);
    x += 4;
    writer.x = x;
    this.writeOpaqueAuth(cred);
    this.writeOpaqueAuth(verf);
    if (params instanceof Uint8Array) {
      if (params.length > 0) writer.buf(params, params.length);
    } else if (params instanceof Reader) {
      const size = params.size();
      if (size > 0) writer.buf(params.subarray(0, size), size);
    }
  }
 
  public writeAcceptedReply(
    xid: number,
    verf: RpcOpaqueAuth,
    acceptStat: number,
    mismatchInfo?: {low: number; high: number},
    results?: Reader | Uint8Array,
  ): void {
    const writer = this.writer;
    writer.ensureCapacity(16 * 4);
    const view = writer.view;
    let x = writer.x;
    view.setUint32(x, xid, false);
    x += 4;
    view.setUint32(x, RpcMsgType.REPLY, false);
    x += 4;
    view.setUint32(x, RpcReplyStat.MSG_ACCEPTED, false);
    x += 4;
    writer.x = x;
    this.writeOpaqueAuth(verf);
    writer.u32(acceptStat);
    if (mismatchInfo) {
      writer.u32(mismatchInfo.low);
      writer.u32(mismatchInfo.high);
    }
    if (results) {
      if (results instanceof Uint8Array) {
        if (results.length > 0) writer.buf(results, results.length);
      } else {
        const size = results.size();
        if (size > 0) writer.buf(results.uint8, size);
      }
    }
  }
 
  public writeRejectedReply(
    xid: number,
    rejectStat: number,
    mismatchInfo?: {low: number; high: number},
    authStat?: number,
  ): void {
    const writer = this.writer;
    writer.ensureCapacity(7 * 4);
    const view = writer.view;
    let x = writer.x;
    view.setUint32(x, xid, false);
    x += 4;
    view.setUint32(x, RpcMsgType.REPLY, false);
    x += 4;
    view.setUint32(x, RpcReplyStat.MSG_DENIED, false);
    x += 4;
    view.setUint32(x, rejectStat, false);
    x += 4;
    if (mismatchInfo) {
      view.setUint32(x, mismatchInfo.low, false);
      x += 4;
      view.setUint32(x, mismatchInfo.high, false);
      x += 4;
    }
    if (authStat !== undefined) {
      view.setUint32(x, authStat, false);
      x += 4;
    }
    writer.x = x;
  }
 
  private writeOpaqueAuth(auth: RpcOpaqueAuth): void {
    const writer = this.writer;
    const body = auth.body;
    const length = body.size();
    Iif (length > 400) throw new RpcEncodingError('Auth body too large');
    writer.ensureCapacity(2 * 4 + length + 3);
    const view = writer.view;
    let x = writer.x;
    view.setUint32(x, auth.flavor, false);
    x += 4;
    view.setUint32(x, length, false);
    x += 4;
    if (length > 0) {
      writer.x = x;
      writer.buf(body.subarray(0, length), length);
      x = writer.x;
      const padding = (4 - (length % 4)) % 4;
      for (let i = 0; i < padding; i++) {
        view.setUint8(x, 0);
        x += 1;
      }
    }
    writer.x = x;
  }
}