XdrSchemaEncoder.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.XdrSchemaEncoder = void 0;
  4. const XdrEncoder_1 = require("./XdrEncoder");
  5. const XdrUnion_1 = require("./XdrUnion");
  6. class XdrSchemaEncoder {
  7. constructor(writer) {
  8. this.writer = writer;
  9. this.encoder = new XdrEncoder_1.XdrEncoder(writer);
  10. }
  11. encode(value, schema) {
  12. this.writer.reset();
  13. this.writeValue(value, schema);
  14. return this.writer.flush();
  15. }
  16. writeVoid(schema) {
  17. this.validateSchemaType(schema, 'void');
  18. this.encoder.writeVoid();
  19. }
  20. writeInt(value, schema) {
  21. this.validateSchemaType(schema, 'int');
  22. if (!Number.isInteger(value) || value < -2147483648 || value > 2147483647) {
  23. throw new Error('Value is not a valid 32-bit signed integer');
  24. }
  25. this.encoder.writeInt(value);
  26. }
  27. writeUnsignedInt(value, schema) {
  28. this.validateSchemaType(schema, 'unsigned_int');
  29. if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
  30. throw new Error('Value is not a valid 32-bit unsigned integer');
  31. }
  32. this.encoder.writeUnsignedInt(value);
  33. }
  34. writeBoolean(value, schema) {
  35. this.validateSchemaType(schema, 'boolean');
  36. this.encoder.writeBoolean(value);
  37. }
  38. writeHyper(value, schema) {
  39. this.validateSchemaType(schema, 'hyper');
  40. this.encoder.writeHyper(value);
  41. }
  42. writeUnsignedHyper(value, schema) {
  43. this.validateSchemaType(schema, 'unsigned_hyper');
  44. if ((typeof value === 'number' && value < 0) || (typeof value === 'bigint' && value < BigInt(0))) {
  45. throw new Error('Value is not a valid unsigned integer');
  46. }
  47. this.encoder.writeUnsignedHyper(value);
  48. }
  49. writeFloat(value, schema) {
  50. this.validateSchemaType(schema, 'float');
  51. this.encoder.writeFloat(value);
  52. }
  53. writeDouble(value, schema) {
  54. this.validateSchemaType(schema, 'double');
  55. this.encoder.writeDouble(value);
  56. }
  57. writeQuadruple(value, schema) {
  58. this.validateSchemaType(schema, 'quadruple');
  59. this.encoder.writeQuadruple(value);
  60. }
  61. writeEnum(value, schema) {
  62. if (schema.type !== 'enum') {
  63. throw new Error('Schema is not an enum schema');
  64. }
  65. if (!(value in schema.values)) {
  66. throw new Error(`Invalid enum value: ${value}. Valid values are: ${Object.keys(schema.values).join(', ')}`);
  67. }
  68. const enumValue = schema.values[value];
  69. if (!Number.isInteger(enumValue)) {
  70. throw new Error(`Enum value ${value} has non-integer assignment: ${enumValue}`);
  71. }
  72. this.encoder.writeInt(enumValue);
  73. }
  74. writeOpaque(value, schema) {
  75. if (schema.type !== 'opaque') {
  76. throw new Error('Schema is not an opaque schema');
  77. }
  78. if (value.length !== schema.size) {
  79. throw new Error(`Opaque data length ${value.length} does not match schema size ${schema.size}`);
  80. }
  81. this.encoder.writeOpaque(value);
  82. }
  83. writeVarlenOpaque(value, schema) {
  84. if (schema.type !== 'vopaque') {
  85. throw new Error('Schema is not a variable-length opaque schema');
  86. }
  87. if (schema.size !== undefined && value.length > schema.size) {
  88. throw new Error(`Opaque data length ${value.length} exceeds maximum size ${schema.size}`);
  89. }
  90. this.encoder.writeVarlenOpaque(value);
  91. }
  92. writeString(value, schema) {
  93. if (schema.type !== 'string') {
  94. throw new Error('Schema is not a string schema');
  95. }
  96. if (schema.size !== undefined && value.length > schema.size) {
  97. throw new Error(`String length ${value.length} exceeds maximum size ${schema.size}`);
  98. }
  99. this.encoder.writeStr(value);
  100. }
  101. writeArray(value, schema) {
  102. if (schema.type !== 'array') {
  103. throw new Error('Schema is not an array schema');
  104. }
  105. if (value.length !== schema.size) {
  106. throw new Error(`Array length ${value.length} does not match schema size ${schema.size}`);
  107. }
  108. for (const item of value) {
  109. this.writeValue(item, schema.elements);
  110. }
  111. }
  112. writeVarlenArray(value, schema) {
  113. if (schema.type !== 'varray') {
  114. throw new Error('Schema is not a variable-length array schema');
  115. }
  116. if (schema.size !== undefined && value.length > schema.size) {
  117. throw new Error(`Array length ${value.length} exceeds maximum size ${schema.size}`);
  118. }
  119. this.encoder.writeUnsignedInt(value.length);
  120. for (const item of value) {
  121. this.writeValue(item, schema.elements);
  122. }
  123. }
  124. writeStruct(value, schema) {
  125. if (schema.type !== 'struct') {
  126. throw new Error('Schema is not a struct schema');
  127. }
  128. for (const [fieldSchema, fieldName] of schema.fields) {
  129. if (!(fieldName in value)) {
  130. throw new Error(`Missing required field: ${fieldName}`);
  131. }
  132. this.writeValue(value[fieldName], fieldSchema);
  133. }
  134. }
  135. writeUnion(value, schema, discriminant) {
  136. if (schema.type !== 'union') {
  137. throw new Error('Schema is not a union schema');
  138. }
  139. const arm = schema.arms.find(([armDiscriminant]) => armDiscriminant === discriminant);
  140. if (!arm) {
  141. if (schema.default) {
  142. this.writeDiscriminant(discriminant);
  143. this.writeValue(value, schema.default);
  144. }
  145. else {
  146. throw new Error(`No matching arm found for discriminant: ${discriminant}`);
  147. }
  148. }
  149. else {
  150. this.writeDiscriminant(discriminant);
  151. this.writeValue(value, arm[1]);
  152. }
  153. }
  154. writeOptional(value, schema) {
  155. if (schema.type !== 'optional') {
  156. throw new Error('Schema is not an optional schema');
  157. }
  158. if (value === null || value === undefined) {
  159. this.encoder.writeBoolean(false);
  160. }
  161. else {
  162. this.encoder.writeBoolean(true);
  163. this.writeValue(value, schema.element);
  164. }
  165. }
  166. writeNumber(value, schema) {
  167. switch (schema.type) {
  168. case 'int':
  169. this.writeInt(value, schema);
  170. break;
  171. case 'unsigned_int':
  172. this.writeUnsignedInt(value, schema);
  173. break;
  174. case 'hyper':
  175. this.writeHyper(value, schema);
  176. break;
  177. case 'unsigned_hyper':
  178. this.writeUnsignedHyper(value, schema);
  179. break;
  180. case 'float':
  181. this.writeFloat(value, schema);
  182. break;
  183. case 'double':
  184. this.writeDouble(value, schema);
  185. break;
  186. case 'quadruple':
  187. this.writeQuadruple(value, schema);
  188. break;
  189. default:
  190. throw new Error(`Schema type ${schema.type} is not a numeric type`);
  191. }
  192. }
  193. writeValue(value, schema) {
  194. switch (schema.type) {
  195. case 'void':
  196. this.encoder.writeVoid();
  197. break;
  198. case 'int':
  199. this.encoder.writeInt(value);
  200. break;
  201. case 'unsigned_int':
  202. this.encoder.writeUnsignedInt(value);
  203. break;
  204. case 'boolean':
  205. this.encoder.writeBoolean(value);
  206. break;
  207. case 'hyper':
  208. this.encoder.writeHyper(value);
  209. break;
  210. case 'unsigned_hyper':
  211. this.encoder.writeUnsignedHyper(value);
  212. break;
  213. case 'float':
  214. this.encoder.writeFloat(value);
  215. break;
  216. case 'double':
  217. this.encoder.writeDouble(value);
  218. break;
  219. case 'quadruple':
  220. this.encoder.writeQuadruple(value);
  221. break;
  222. case 'enum':
  223. this.writeEnum(value, schema);
  224. break;
  225. case 'opaque':
  226. this.writeOpaque(value, schema);
  227. break;
  228. case 'vopaque':
  229. this.writeVarlenOpaque(value, schema);
  230. break;
  231. case 'string':
  232. this.writeString(value, schema);
  233. break;
  234. case 'array':
  235. this.writeArray(value, schema);
  236. break;
  237. case 'varray':
  238. this.writeVarlenArray(value, schema);
  239. break;
  240. case 'struct':
  241. this.writeStruct(value, schema);
  242. break;
  243. case 'union':
  244. if (value instanceof XdrUnion_1.XdrUnion) {
  245. this.writeUnion(value.value, schema, value.discriminant);
  246. }
  247. else {
  248. throw new Error('Union values must be wrapped in XdrUnion class');
  249. }
  250. break;
  251. case 'optional':
  252. this.writeOptional(value, schema);
  253. break;
  254. case 'const':
  255. break;
  256. default:
  257. throw new Error(`Unknown schema type: ${schema.type}`);
  258. }
  259. }
  260. validateSchemaType(schema, expectedType) {
  261. if (schema.type !== expectedType) {
  262. throw new Error(`Expected schema type ${expectedType}, got ${schema.type}`);
  263. }
  264. }
  265. writeDiscriminant(discriminant) {
  266. if (typeof discriminant === 'number') {
  267. this.encoder.writeInt(discriminant);
  268. }
  269. else if (typeof discriminant === 'boolean') {
  270. this.encoder.writeBoolean(discriminant);
  271. }
  272. else {
  273. throw new Error('String discriminants require enum schema context');
  274. }
  275. }
  276. }
  277. exports.XdrSchemaEncoder = XdrSchemaEncoder;
  278. //# sourceMappingURL=XdrSchemaEncoder.js.map