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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | 2x 2x 2x 80x 80x 54x 54x 50x 2x 2x 5x 4x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 6x 1x 5x 1x 4x 4x 4x 3x 3x 1x 2x 4x 4x 1x 3x 19x 19x 1x 18x 9x 9x 1x 8x 18x 7x 7x 1x 6x 6x 11x 14x 14x 27x 1x 26x 11x 13x 11x 2x 1x 1x 1x 9x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 118x 2x 2x 38x 38x 2x 2x 4x 4x 2x 2x 2x 2x 3x 3x 2x 2x 2x 4x 4x 2x 2x 3x 3x 18x 18x 8x 8x 6x 6x 13x 13x 6x 5x 1x 5x 1x 15x 1x 10x 8x 2x 1x 1x | import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
import {XdrEncoder} from './XdrEncoder';
import {XdrUnion} from './XdrUnion';
import type {
XdrSchema,
XdrEnumSchema,
XdrOpaqueSchema,
XdrVarlenOpaqueSchema,
XdrStringSchema,
XdrArraySchema,
XdrVarlenArraySchema,
XdrStructSchema,
XdrUnionSchema,
XdrOptionalSchema,
} from './types';
export class XdrSchemaEncoder {
private encoder: XdrEncoder;
constructor(public readonly writer: IWriter & IWriterGrowable) {
this.encoder = new XdrEncoder(writer);
}
public encode(value: unknown, schema: XdrSchema): Uint8Array {
this.writer.reset();
this.writeValue(value, schema);
return this.writer.flush();
}
public writeVoid(schema: XdrSchema): void {
this.validateSchemaType(schema, 'void');
this.encoder.writeVoid();
}
public writeInt(value: number, schema: XdrSchema): void {
this.validateSchemaType(schema, 'int');
if (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
throw new Error('Value is not a valid 32-bit signed integer');
}
this.encoder.writeInt(value);
}
public writeUnsignedInt(value: number, schema: XdrSchema): void {
this.validateSchemaType(schema, 'unsigned_int');
if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
throw new Error('Value is not a valid 32-bit unsigned integer');
}
this.encoder.writeUnsignedInt(value);
}
public writeBoolean(value: boolean, schema: XdrSchema): void {
this.validateSchemaType(schema, 'boolean');
this.encoder.writeBoolean(value);
}
public writeHyper(value: number | bigint, schema: XdrSchema): void {
this.validateSchemaType(schema, 'hyper');
this.encoder.writeHyper(value);
}
public writeUnsignedHyper(value: number | bigint, schema: XdrSchema): void {
this.validateSchemaType(schema, 'unsigned_hyper');
if ((typeof value === 'number' && value < 0) || (typeof value === 'bigint' && value < BigInt(0))) {
throw new Error('Value is not a valid unsigned integer');
}
this.encoder.writeUnsignedHyper(value);
}
public writeFloat(value: number, schema: XdrSchema): void {
this.validateSchemaType(schema, 'float');
this.encoder.writeFloat(value);
}
public writeDouble(value: number, schema: XdrSchema): void {
this.validateSchemaType(schema, 'double');
this.encoder.writeDouble(value);
}
public writeQuadruple(value: number, schema: XdrSchema): void {
this.validateSchemaType(schema, 'quadruple');
this.encoder.writeQuadruple(value);
}
public writeEnum(value: string, schema: XdrEnumSchema): void {
if (schema.type !== 'enum') {
throw new Error('Schema is not an enum schema');
}
if (!(value in schema.values)) {
throw new Error(`Invalid enum value: ${value}. Valid values are: ${Object.keys(schema.values).join(', ')}`);
}
const enumValue = schema.values[value];
// Per RFC 4506 Section 4.3: "It is an error to encode as an enum any integer other than those that have been given assignments"
Iif (!Number.isInteger(enumValue)) {
throw new Error(`Enum value ${value} has non-integer assignment: ${enumValue}`);
}
this.encoder.writeInt(enumValue);
}
public writeOpaque(value: Uint8Array, schema: XdrOpaqueSchema): void {
Iif (schema.type !== 'opaque') {
throw new Error('Schema is not an opaque schema');
}
if (value.length !== schema.size) {
throw new Error(`Opaque data length ${value.length} does not match schema size ${schema.size}`);
}
this.encoder.writeOpaque(value);
}
public writeVarlenOpaque(value: Uint8Array, schema: XdrVarlenOpaqueSchema): void {
Iif (schema.type !== 'vopaque') {
throw new Error('Schema is not a variable-length opaque schema');
}
if (schema.size !== undefined && value.length > schema.size) {
throw new Error(`Opaque data length ${value.length} exceeds maximum size ${schema.size}`);
}
this.encoder.writeVarlenOpaque(value);
}
public writeString(value: string, schema: XdrStringSchema): void {
Iif (schema.type !== 'string') {
throw new Error('Schema is not a string schema');
}
if (schema.size !== undefined && value.length > schema.size) {
throw new Error(`String length ${value.length} exceeds maximum size ${schema.size}`);
}
this.encoder.writeStr(value);
}
public writeArray(value: unknown[], schema: XdrArraySchema): void {
Iif (schema.type !== 'array') {
throw new Error('Schema is not an array schema');
}
if (value.length !== schema.size) {
throw new Error(`Array length ${value.length} does not match schema size ${schema.size}`);
}
for (const item of value) {
this.writeValue(item, schema.elements);
}
}
public writeVarlenArray(value: unknown[], schema: XdrVarlenArraySchema): void {
Iif (schema.type !== 'varray') {
throw new Error('Schema is not a variable-length array schema');
}
if (schema.size !== undefined && value.length > schema.size) {
throw new Error(`Array length ${value.length} exceeds maximum size ${schema.size}`);
}
this.encoder.writeUnsignedInt(value.length);
for (const item of value) {
this.writeValue(item, schema.elements);
}
}
public writeStruct(value: Record<string, unknown>, schema: XdrStructSchema): void {
Iif (schema.type !== 'struct') {
throw new Error('Schema is not a struct schema');
}
for (const [fieldSchema, fieldName] of schema.fields) {
if (!(fieldName in value)) {
throw new Error(`Missing required field: ${fieldName}`);
}
this.writeValue(value[fieldName], fieldSchema);
}
}
public writeUnion(value: unknown, schema: XdrUnionSchema, discriminant: number | string | boolean): void {
Iif (schema.type !== 'union') {
throw new Error('Schema is not a union schema');
}
const arm = schema.arms.find(([armDiscriminant]) => armDiscriminant === discriminant);
if (!arm) {
if (schema.default) {
this.writeDiscriminant(discriminant);
this.writeValue(value, schema.default);
} else {
throw new Error(`No matching arm found for discriminant: ${discriminant}`);
}
} else {
this.writeDiscriminant(discriminant);
this.writeValue(value, arm[1]);
}
}
/**
* Writes optional-data value (RFC 1832 Section 3.19).
* Optional-data is syntactic sugar for a union with boolean discriminant.
* If value is null/undefined, writes FALSE; otherwise writes TRUE and the value.
*/
public writeOptional(value: unknown, schema: XdrOptionalSchema): void {
Iif (schema.type !== 'optional') {
throw new Error('Schema is not an optional schema');
}
if (value === null || value === undefined) {
this.encoder.writeBoolean(false);
} else {
this.encoder.writeBoolean(true);
this.writeValue(value, schema.element);
}
}
public writeNumber(value: number, schema: XdrSchema): void {
switch (schema.type) {
case 'int':
this.writeInt(value, schema);
break;
case 'unsigned_int':
this.writeUnsignedInt(value, schema);
break;
case 'hyper':
this.writeHyper(value, schema);
break;
case 'unsigned_hyper':
this.writeUnsignedHyper(value, schema);
break;
case 'float':
this.writeFloat(value, schema);
break;
case 'double':
this.writeDouble(value, schema);
break;
case 'quadruple':
this.writeQuadruple(value, schema);
break;
default:
throw new Error(`Schema type ${schema.type} is not a numeric type`);
}
}
private writeValue(value: unknown, schema: XdrSchema): void {
switch (schema.type) {
case 'void':
this.encoder.writeVoid();
break;
case 'int':
this.encoder.writeInt(value as number);
break;
case 'unsigned_int':
this.encoder.writeUnsignedInt(value as number);
break;
case 'boolean':
this.encoder.writeBoolean(value as boolean);
break;
case 'hyper':
this.encoder.writeHyper(value as number | bigint);
break;
case 'unsigned_hyper':
this.encoder.writeUnsignedHyper(value as number | bigint);
break;
case 'float':
this.encoder.writeFloat(value as number);
break;
case 'double':
this.encoder.writeDouble(value as number);
break;
case 'quadruple':
this.encoder.writeQuadruple(value as number);
break;
case 'enum':
this.writeEnum(value as string, schema as XdrEnumSchema);
break;
case 'opaque':
this.writeOpaque(value as Uint8Array, schema as XdrOpaqueSchema);
break;
case 'vopaque':
this.writeVarlenOpaque(value as Uint8Array, schema as XdrVarlenOpaqueSchema);
break;
case 'string':
this.writeString(value as string, schema as XdrStringSchema);
break;
case 'array':
this.writeArray(value as unknown[], schema as XdrArraySchema);
break;
case 'varray':
this.writeVarlenArray(value as unknown[], schema as XdrVarlenArraySchema);
break;
case 'struct':
this.writeStruct(value as Record<string, unknown>, schema as XdrStructSchema);
break;
case 'union':
if (value instanceof XdrUnion) {
this.writeUnion(value.value, schema as XdrUnionSchema, value.discriminant);
} else {
throw new Error('Union values must be wrapped in XdrUnion class');
}
break;
case 'optional':
this.writeOptional(value, schema as XdrOptionalSchema);
break;
case 'const':
// Constants are not encoded; they are compile-time values
break;
default:
throw new Error(`Unknown schema type: ${(schema as any).type}`);
}
}
private validateSchemaType(schema: XdrSchema, expectedType: string): void {
if (schema.type !== expectedType) {
throw new Error(`Expected schema type ${expectedType}, got ${schema.type}`);
}
}
private writeDiscriminant(discriminant: number | string | boolean): void {
if (typeof discriminant === 'number') {
this.encoder.writeInt(discriminant);
} else if (typeof discriminant === 'boolean') {
this.encoder.writeBoolean(discriminant);
} else {
// For string discriminants, we need to know the enum mapping
// This is a simplified implementation
throw new Error('String discriminants require enum schema context');
}
}
}
|