All files / json-pack/src/xdr XdrEncoder.ts

95.74% Statements 90/94
95.45% Branches 21/22
87.5% Functions 21/24
95.45% Lines 84/88

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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257                          38x 1310x     8x 8x 8x 8x             1x       8x   1x   1x   1x   2x 1x 1x   1x           1x   1x   1x                                           704x             812x 812x 812x 812x             17938x 17938x 17938x 17938x             132x 132x 132x 129x   3x 3x 3x 3x 3x   132x             1904x 1904x   1904x 1903x   1x 1x 1x 1x 1x   1904x             14x 14x 14x 14x             14x 14x 14x 14x               4x               2222x 2222x 2222x 2222x 2222x 2222x 2222x               1284x 1284x               2423x     2423x 2423x 2423x 2423x 2423x 2423x     2423x 2423x 2423x 2423x                                 4x 3x 1x 1x             1x             1x             2x             1x      
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
import type {BinaryJsonEncoder} from '../types';
 
/**
 * XDR (External Data Representation) binary encoder for basic value encoding.
 * Implements XDR binary encoding according to RFC 4506.
 *
 * Key XDR encoding principles:
 * - All data types are aligned to 4-byte boundaries
 * - Multi-byte quantities are transmitted in big-endian byte order
 * - Strings and opaque data are padded to 4-byte boundaries
 * - Variable-length arrays and strings are preceded by their length
 */
export class XdrEncoder implements BinaryJsonEncoder {
  constructor(public readonly writer: IWriter & IWriterGrowable) {}
 
  public encode(value: unknown): Uint8Array {
    const writer = this.writer;
    writer.reset();
    this.writeAny(value);
    return writer.flush();
  }
 
  /**
   * Called when the encoder encounters a value that it does not know how to encode.
   */
  public writeUnknown(value: unknown): void {
    this.writeVoid();
  }
 
  public writeAny(value: unknown): void {
    switch (typeof value) {
      case 'boolean':
        return this.writeBoolean(value);
      case 'number':
        return this.writeNumber(value);
      case 'string':
        return this.writeStr(value);
      case 'object': {
        if (value === null) return this.writeVoid();
        const construct = value.constructor;
        switch (construct) {
          case Uint8Array:
            return this.writeBin(value as Uint8Array);
          default:
            return this.writeUnknown(value);
        }
      }
      case 'bigint':
        return this.writeHyper(value);
      case 'undefined':
        return this.writeVoid();
      default:
        return this.writeUnknown(value);
    }
  }
 
  /**
   * Writes an XDR void value (no data is actually written).
   */
  public writeVoid(): void {
    // Void values are encoded as no data
  }
 
  /**
   * Writes an XDR null value (for interface compatibility).
   */
  public writeNull(): void {
    this.writeVoid();
  }
 
  /**
   * Writes an XDR boolean value as a 4-byte integer.
   */
  public writeBoolean(bool: boolean): void {
    this.writeInt(bool ? 1 : 0);
  }
 
  /**
   * Writes an XDR signed 32-bit integer in big-endian format.
   */
  public writeInt(int: number): void {
    const writer = this.writer;
    writer.ensureCapacity(4);
    writer.view.setInt32(writer.x, Math.trunc(int), false); // big-endian
    writer.move(4);
  }
 
  /**
   * Writes an XDR unsigned 32-bit integer in big-endian format.
   */
  public writeUnsignedInt(uint: number): void {
    const writer = this.writer;
    writer.ensureCapacity(4);
    writer.view.setUint32(writer.x, Math.trunc(uint) >>> 0, false); // big-endian
    writer.move(4);
  }
 
  /**
   * Writes an XDR signed 64-bit integer (hyper) in big-endian format.
   */
  public writeHyper(hyper: number | bigint): void {
    const writer = this.writer;
    writer.ensureCapacity(8);
    if (typeof hyper === 'bigint') {
      writer.view.setBigInt64(writer.x, hyper, false); // big-endian
    } else {
      const truncated = Math.trunc(hyper);
      const high = Math.floor(truncated / 0x100000000);
      const low = truncated >>> 0;
      writer.view.setInt32(writer.x, high, false); // high 32 bits
      writer.view.setUint32(writer.x + 4, low, false); // low 32 bits
    }
    writer.move(8);
  }
 
  /**
   * Writes an XDR unsigned 64-bit integer (unsigned hyper) in big-endian format.
   */
  public writeUnsignedHyper(uhyper: number | bigint): void {
    const writer = this.writer;
    writer.ensureCapacity(8);
 
    if (typeof uhyper === 'bigint') {
      writer.view.setBigUint64(writer.x, uhyper, false); // big-endian
    } else {
      const truncated = Math.trunc(Math.abs(uhyper));
      const high = Math.floor(truncated / 0x100000000);
      const low = truncated >>> 0;
      writer.view.setUint32(writer.x, high, false); // high 32 bits
      writer.view.setUint32(writer.x + 4, low, false); // low 32 bits
    }
    writer.move(8);
  }
 
  /**
   * Writes an XDR float value using IEEE 754 single-precision in big-endian format.
   */
  public writeFloat(float: number): void {
    const writer = this.writer;
    writer.ensureCapacity(4);
    writer.view.setFloat32(writer.x, float, false); // big-endian
    writer.move(4);
  }
 
  /**
   * Writes an XDR double value using IEEE 754 double-precision in big-endian format.
   */
  public writeDouble(double: number): void {
    const writer = this.writer;
    writer.ensureCapacity(8);
    writer.view.setFloat64(writer.x, double, false); // big-endian
    writer.move(8);
  }
 
  /**
   * Writes an XDR quadruple value (128-bit float).
   * Note: JavaScript doesn't have native 128-bit float support.
   */
  public writeQuadruple(quad: number): void {
    throw new Error('not implemented');
  }
 
  /**
   * Writes XDR opaque data with fixed length.
   * Data is padded to 4-byte boundary.
   */
  public writeOpaque(data: Uint8Array): void {
    const size = data.length;
    const writer = this.writer;
    const paddedSize = Math.ceil(size / 4) * 4;
    writer.ensureCapacity(paddedSize);
    writer.buf(data, size);
    const padding = paddedSize - size;
    for (let i = 0; i < padding; i++) writer.u8(0);
  }
 
  /**
   * Writes XDR variable-length opaque data.
   * Length is written first, followed by data padded to 4-byte boundary.
   */
  public writeVarlenOpaque(data: Uint8Array): void {
    this.writeUnsignedInt(data.length);
    this.writeOpaque(data);
  }
 
  /**
   * Writes an XDR string with UTF-8 encoding.
   * Length is written first, followed by UTF-8 bytes padded to 4-byte boundary.
   */
  public writeStr(str: string): void {
    const writer = this.writer;
 
    // Write string using writer's UTF-8 method and get actual byte count
    const lengthOffset = writer.x;
    writer.x += 4; // Reserve space for length
    const bytesWritten = writer.utf8(str);
    const paddedSize = Math.ceil(bytesWritten / 4) * 4;
    const padding = paddedSize - bytesWritten;
    for (let i = 0; i < padding; i++) writer.u8(0);
 
    // Go back and write the actual byte length
    const currentPos = writer.x;
    writer.x = lengthOffset;
    this.writeUnsignedInt(bytesWritten);
    writer.x = currentPos;
  }
 
  public writeArr(arr: unknown[]): void {
    throw new Error('not implemented');
  }
 
  public writeObj(obj: Record<string, unknown>): void {
    throw new Error('not implemented');
  }
 
  // BinaryJsonEncoder interface methods
 
  /**
   * Generic number writing - determines type based on value
   */
  public writeNumber(num: number): void {
    if (Number.isInteger(num)) {
      if (num >= -2147483648 && num <= 2147483647) this.writeInt(num);
      else this.writeHyper(num);
    } else this.writeDouble(num);
  }
 
  /**
   * Writes an integer value
   */
  public writeInteger(int: number): void {
    this.writeInt(int);
  }
 
  /**
   * Writes an unsigned integer value
   */
  public writeUInteger(uint: number): void {
    this.writeUnsignedInt(uint);
  }
 
  /**
   * Writes binary data
   */
  public writeBin(buf: Uint8Array): void {
    this.writeVarlenOpaque(buf);
  }
 
  /**
   * Writes an ASCII string (same as regular string in XDR)
   */
  public writeAsciiStr(str: string): void {
    this.writeStr(str);
  }
}