XdrSchemaEncoder.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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}`);
  67. }
  68. this.encoder.writeInt(schema.values[value]);
  69. }
  70. writeOpaque(value, schema) {
  71. if (schema.type !== 'opaque') {
  72. throw new Error('Schema is not an opaque schema');
  73. }
  74. if (value.length !== schema.size) {
  75. throw new Error(`Opaque data length ${value.length} does not match schema size ${schema.size}`);
  76. }
  77. this.encoder.writeOpaque(value);
  78. }
  79. writeVarlenOpaque(value, schema) {
  80. if (schema.type !== 'vopaque') {
  81. throw new Error('Schema is not a variable-length opaque schema');
  82. }
  83. if (schema.size !== undefined && value.length > schema.size) {
  84. throw new Error(`Opaque data length ${value.length} exceeds maximum size ${schema.size}`);
  85. }
  86. this.encoder.writeVarlenOpaque(value);
  87. }
  88. writeString(value, schema) {
  89. if (schema.type !== 'string') {
  90. throw new Error('Schema is not a string schema');
  91. }
  92. if (schema.size !== undefined && value.length > schema.size) {
  93. throw new Error(`String length ${value.length} exceeds maximum size ${schema.size}`);
  94. }
  95. this.encoder.writeStr(value);
  96. }
  97. writeArray(value, schema) {
  98. if (schema.type !== 'array') {
  99. throw new Error('Schema is not an array schema');
  100. }
  101. if (value.length !== schema.size) {
  102. throw new Error(`Array length ${value.length} does not match schema size ${schema.size}`);
  103. }
  104. for (const item of value) {
  105. this.writeValue(item, schema.elements);
  106. }
  107. }
  108. writeVarlenArray(value, schema) {
  109. if (schema.type !== 'varray') {
  110. throw new Error('Schema is not a variable-length array schema');
  111. }
  112. if (schema.size !== undefined && value.length > schema.size) {
  113. throw new Error(`Array length ${value.length} exceeds maximum size ${schema.size}`);
  114. }
  115. this.encoder.writeUnsignedInt(value.length);
  116. for (const item of value) {
  117. this.writeValue(item, schema.elements);
  118. }
  119. }
  120. writeStruct(value, schema) {
  121. if (schema.type !== 'struct') {
  122. throw new Error('Schema is not a struct schema');
  123. }
  124. for (const [fieldSchema, fieldName] of schema.fields) {
  125. if (!(fieldName in value)) {
  126. throw new Error(`Missing required field: ${fieldName}`);
  127. }
  128. this.writeValue(value[fieldName], fieldSchema);
  129. }
  130. }
  131. writeUnion(value, schema, discriminant) {
  132. if (schema.type !== 'union') {
  133. throw new Error('Schema is not a union schema');
  134. }
  135. const arm = schema.arms.find(([armDiscriminant]) => armDiscriminant === discriminant);
  136. if (!arm) {
  137. if (schema.default) {
  138. this.writeDiscriminant(discriminant);
  139. this.writeValue(value, schema.default);
  140. }
  141. else {
  142. throw new Error(`No matching arm found for discriminant: ${discriminant}`);
  143. }
  144. }
  145. else {
  146. this.writeDiscriminant(discriminant);
  147. this.writeValue(value, arm[1]);
  148. }
  149. }
  150. writeNumber(value, schema) {
  151. switch (schema.type) {
  152. case 'int':
  153. this.writeInt(value, schema);
  154. break;
  155. case 'unsigned_int':
  156. this.writeUnsignedInt(value, schema);
  157. break;
  158. case 'hyper':
  159. this.writeHyper(value, schema);
  160. break;
  161. case 'unsigned_hyper':
  162. this.writeUnsignedHyper(value, schema);
  163. break;
  164. case 'float':
  165. this.writeFloat(value, schema);
  166. break;
  167. case 'double':
  168. this.writeDouble(value, schema);
  169. break;
  170. case 'quadruple':
  171. this.writeQuadruple(value, schema);
  172. break;
  173. default:
  174. throw new Error(`Schema type ${schema.type} is not a numeric type`);
  175. }
  176. }
  177. writeValue(value, schema) {
  178. switch (schema.type) {
  179. case 'void':
  180. this.encoder.writeVoid();
  181. break;
  182. case 'int':
  183. this.encoder.writeInt(value);
  184. break;
  185. case 'unsigned_int':
  186. this.encoder.writeUnsignedInt(value);
  187. break;
  188. case 'boolean':
  189. this.encoder.writeBoolean(value);
  190. break;
  191. case 'hyper':
  192. this.encoder.writeHyper(value);
  193. break;
  194. case 'unsigned_hyper':
  195. this.encoder.writeUnsignedHyper(value);
  196. break;
  197. case 'float':
  198. this.encoder.writeFloat(value);
  199. break;
  200. case 'double':
  201. this.encoder.writeDouble(value);
  202. break;
  203. case 'quadruple':
  204. this.encoder.writeQuadruple(value);
  205. break;
  206. case 'enum':
  207. this.writeEnum(value, schema);
  208. break;
  209. case 'opaque':
  210. this.writeOpaque(value, schema);
  211. break;
  212. case 'vopaque':
  213. this.writeVarlenOpaque(value, schema);
  214. break;
  215. case 'string':
  216. this.writeString(value, schema);
  217. break;
  218. case 'array':
  219. this.writeArray(value, schema);
  220. break;
  221. case 'varray':
  222. this.writeVarlenArray(value, schema);
  223. break;
  224. case 'struct':
  225. this.writeStruct(value, schema);
  226. break;
  227. case 'union':
  228. if (value instanceof XdrUnion_1.XdrUnion) {
  229. this.writeUnion(value.value, schema, value.discriminant);
  230. }
  231. else {
  232. throw new Error('Union values must be wrapped in XdrUnion class');
  233. }
  234. break;
  235. default:
  236. throw new Error(`Unknown schema type: ${schema.type}`);
  237. }
  238. }
  239. validateSchemaType(schema, expectedType) {
  240. if (schema.type !== expectedType) {
  241. throw new Error(`Expected schema type ${expectedType}, got ${schema.type}`);
  242. }
  243. }
  244. writeDiscriminant(discriminant) {
  245. if (typeof discriminant === 'number') {
  246. this.encoder.writeInt(discriminant);
  247. }
  248. else if (typeof discriminant === 'boolean') {
  249. this.encoder.writeBoolean(discriminant);
  250. }
  251. else {
  252. throw new Error('String discriminants require enum schema context');
  253. }
  254. }
  255. }
  256. exports.XdrSchemaEncoder = XdrSchemaEncoder;
  257. //# sourceMappingURL=XdrSchemaEncoder.js.map