stream-parser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _jsbi = _interopRequireDefault(require("jsbi"));
  7. var _readableStream = require("readable-stream");
  8. var _token = require("./token");
  9. var _colmetadataTokenParser = _interopRequireDefault(require("./colmetadata-token-parser"));
  10. var _doneTokenParser = require("./done-token-parser");
  11. var _envChangeTokenParser = _interopRequireDefault(require("./env-change-token-parser"));
  12. var _infoerrorTokenParser = require("./infoerror-token-parser");
  13. var _fedauthInfoParser = _interopRequireDefault(require("./fedauth-info-parser"));
  14. var _featureExtAckParser = _interopRequireDefault(require("./feature-ext-ack-parser"));
  15. var _loginackTokenParser = _interopRequireDefault(require("./loginack-token-parser"));
  16. var _orderTokenParser = _interopRequireDefault(require("./order-token-parser"));
  17. var _returnstatusTokenParser = _interopRequireDefault(require("./returnstatus-token-parser"));
  18. var _returnvalueTokenParser = _interopRequireDefault(require("./returnvalue-token-parser"));
  19. var _rowTokenParser = _interopRequireDefault(require("./row-token-parser"));
  20. var _nbcrowTokenParser = _interopRequireDefault(require("./nbcrow-token-parser"));
  21. var _sspiTokenParser = _interopRequireDefault(require("./sspi-token-parser"));
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. const tokenParsers = {
  24. [_token.TYPE.COLMETADATA]: _colmetadataTokenParser.default,
  25. [_token.TYPE.DONE]: _doneTokenParser.doneParser,
  26. [_token.TYPE.DONEINPROC]: _doneTokenParser.doneInProcParser,
  27. [_token.TYPE.DONEPROC]: _doneTokenParser.doneProcParser,
  28. [_token.TYPE.ENVCHANGE]: _envChangeTokenParser.default,
  29. [_token.TYPE.ERROR]: _infoerrorTokenParser.errorParser,
  30. [_token.TYPE.FEDAUTHINFO]: _fedauthInfoParser.default,
  31. [_token.TYPE.FEATUREEXTACK]: _featureExtAckParser.default,
  32. [_token.TYPE.INFO]: _infoerrorTokenParser.infoParser,
  33. [_token.TYPE.LOGINACK]: _loginackTokenParser.default,
  34. [_token.TYPE.ORDER]: _orderTokenParser.default,
  35. [_token.TYPE.RETURNSTATUS]: _returnstatusTokenParser.default,
  36. [_token.TYPE.RETURNVALUE]: _returnvalueTokenParser.default,
  37. [_token.TYPE.ROW]: _rowTokenParser.default,
  38. [_token.TYPE.NBCROW]: _nbcrowTokenParser.default,
  39. [_token.TYPE.SSPI]: _sspiTokenParser.default
  40. };
  41. class EndOfMessageMarker {}
  42. class Parser extends _readableStream.Transform {
  43. constructor(debug, colMetadata, options) {
  44. super({
  45. objectMode: true
  46. });
  47. this.debug = debug;
  48. this.colMetadata = colMetadata;
  49. this.options = options;
  50. this.endOfMessageMarker = new EndOfMessageMarker();
  51. this.buffer = Buffer.alloc(0);
  52. this.position = 0;
  53. this.suspended = false;
  54. this.next = undefined;
  55. }
  56. _transform(input, _encoding, done) {
  57. if (input instanceof EndOfMessageMarker) {
  58. return done(undefined, new _token.EndOfMessageToken());
  59. }
  60. if (this.position === this.buffer.length) {
  61. this.buffer = input;
  62. } else {
  63. this.buffer = Buffer.concat([this.buffer.slice(this.position), input]);
  64. }
  65. this.position = 0;
  66. if (this.suspended) {
  67. // Unsuspend and continue from where ever we left off.
  68. this.suspended = false;
  69. const next = this.next;
  70. next();
  71. } // If we're no longer suspended, parse new tokens
  72. if (!this.suspended) {
  73. // Start the parser
  74. this.parseTokens();
  75. }
  76. done();
  77. }
  78. parseTokens() {
  79. const doneParsing = token => {
  80. if (token) {
  81. if (token instanceof _token.ColMetadataToken) {
  82. this.colMetadata = token.columns;
  83. }
  84. this.push(token);
  85. }
  86. };
  87. while (!this.suspended && this.position + 1 <= this.buffer.length) {
  88. const type = this.buffer.readUInt8(this.position);
  89. this.position += 1;
  90. if (tokenParsers[type]) {
  91. tokenParsers[type](this, this.colMetadata, this.options, doneParsing);
  92. } else {
  93. this.emit('error', new Error('Unknown type: ' + type));
  94. }
  95. }
  96. }
  97. suspend(next) {
  98. this.suspended = true;
  99. this.next = next;
  100. }
  101. awaitData(length, callback) {
  102. if (this.position + length <= this.buffer.length) {
  103. callback();
  104. } else {
  105. this.suspend(() => {
  106. this.awaitData(length, callback);
  107. });
  108. }
  109. }
  110. readInt8(callback) {
  111. this.awaitData(1, () => {
  112. const data = this.buffer.readInt8(this.position);
  113. this.position += 1;
  114. callback(data);
  115. });
  116. }
  117. readUInt8(callback) {
  118. this.awaitData(1, () => {
  119. const data = this.buffer.readUInt8(this.position);
  120. this.position += 1;
  121. callback(data);
  122. });
  123. }
  124. readInt16LE(callback) {
  125. this.awaitData(2, () => {
  126. const data = this.buffer.readInt16LE(this.position);
  127. this.position += 2;
  128. callback(data);
  129. });
  130. }
  131. readInt16BE(callback) {
  132. this.awaitData(2, () => {
  133. const data = this.buffer.readInt16BE(this.position);
  134. this.position += 2;
  135. callback(data);
  136. });
  137. }
  138. readUInt16LE(callback) {
  139. this.awaitData(2, () => {
  140. const data = this.buffer.readUInt16LE(this.position);
  141. this.position += 2;
  142. callback(data);
  143. });
  144. }
  145. readUInt16BE(callback) {
  146. this.awaitData(2, () => {
  147. const data = this.buffer.readUInt16BE(this.position);
  148. this.position += 2;
  149. callback(data);
  150. });
  151. }
  152. readInt32LE(callback) {
  153. this.awaitData(4, () => {
  154. const data = this.buffer.readInt32LE(this.position);
  155. this.position += 4;
  156. callback(data);
  157. });
  158. }
  159. readInt32BE(callback) {
  160. this.awaitData(4, () => {
  161. const data = this.buffer.readInt32BE(this.position);
  162. this.position += 4;
  163. callback(data);
  164. });
  165. }
  166. readUInt32LE(callback) {
  167. this.awaitData(4, () => {
  168. const data = this.buffer.readUInt32LE(this.position);
  169. this.position += 4;
  170. callback(data);
  171. });
  172. }
  173. readUInt32BE(callback) {
  174. this.awaitData(4, () => {
  175. const data = this.buffer.readUInt32BE(this.position);
  176. this.position += 4;
  177. callback(data);
  178. });
  179. }
  180. readBigInt64LE(callback) {
  181. this.awaitData(8, () => {
  182. const result = _jsbi.default.add(_jsbi.default.leftShift(_jsbi.default.BigInt(this.buffer[this.position + 4] + this.buffer[this.position + 5] * Math.pow(2, 8) + this.buffer[this.position + 6] * Math.pow(2, 16) + (this.buffer[this.position + 7] << 24) // Overflow
  183. ), _jsbi.default.BigInt(32)), _jsbi.default.BigInt(this.buffer[this.position] + this.buffer[this.position + 1] * Math.pow(2, 8) + this.buffer[this.position + 2] * Math.pow(2, 16) + this.buffer[this.position + 3] * Math.pow(2, 24)));
  184. this.position += 8;
  185. callback(result);
  186. });
  187. }
  188. readInt64LE(callback) {
  189. this.awaitData(8, () => {
  190. const data = Math.pow(2, 32) * this.buffer.readInt32LE(this.position + 4) + ((this.buffer[this.position + 4] & 0x80) === 0x80 ? 1 : -1) * this.buffer.readUInt32LE(this.position);
  191. this.position += 8;
  192. callback(data);
  193. });
  194. }
  195. readInt64BE(callback) {
  196. this.awaitData(8, () => {
  197. const data = Math.pow(2, 32) * this.buffer.readInt32BE(this.position) + ((this.buffer[this.position] & 0x80) === 0x80 ? 1 : -1) * this.buffer.readUInt32BE(this.position + 4);
  198. this.position += 8;
  199. callback(data);
  200. });
  201. }
  202. readBigUInt64LE(callback) {
  203. this.awaitData(8, () => {
  204. const low = _jsbi.default.BigInt(this.buffer.readUInt32LE(this.position));
  205. const high = _jsbi.default.BigInt(this.buffer.readUInt32LE(this.position + 4));
  206. this.position += 8;
  207. callback(_jsbi.default.add(low, _jsbi.default.leftShift(high, _jsbi.default.BigInt(32))));
  208. });
  209. }
  210. readUInt64LE(callback) {
  211. this.awaitData(8, () => {
  212. const data = Math.pow(2, 32) * this.buffer.readUInt32LE(this.position + 4) + this.buffer.readUInt32LE(this.position);
  213. this.position += 8;
  214. callback(data);
  215. });
  216. }
  217. readUInt64BE(callback) {
  218. this.awaitData(8, () => {
  219. const data = Math.pow(2, 32) * this.buffer.readUInt32BE(this.position) + this.buffer.readUInt32BE(this.position + 4);
  220. this.position += 8;
  221. callback(data);
  222. });
  223. }
  224. readFloatLE(callback) {
  225. this.awaitData(4, () => {
  226. const data = this.buffer.readFloatLE(this.position);
  227. this.position += 4;
  228. callback(data);
  229. });
  230. }
  231. readFloatBE(callback) {
  232. this.awaitData(4, () => {
  233. const data = this.buffer.readFloatBE(this.position);
  234. this.position += 4;
  235. callback(data);
  236. });
  237. }
  238. readDoubleLE(callback) {
  239. this.awaitData(8, () => {
  240. const data = this.buffer.readDoubleLE(this.position);
  241. this.position += 8;
  242. callback(data);
  243. });
  244. }
  245. readDoubleBE(callback) {
  246. this.awaitData(8, () => {
  247. const data = this.buffer.readDoubleBE(this.position);
  248. this.position += 8;
  249. callback(data);
  250. });
  251. }
  252. readUInt24LE(callback) {
  253. this.awaitData(3, () => {
  254. const low = this.buffer.readUInt16LE(this.position);
  255. const high = this.buffer.readUInt8(this.position + 2);
  256. this.position += 3;
  257. callback(low | high << 16);
  258. });
  259. }
  260. readUInt40LE(callback) {
  261. this.awaitData(5, () => {
  262. const low = this.buffer.readUInt32LE(this.position);
  263. const high = this.buffer.readUInt8(this.position + 4);
  264. this.position += 5;
  265. callback(0x100000000 * high + low);
  266. });
  267. }
  268. readUNumeric64LE(callback) {
  269. this.awaitData(8, () => {
  270. const low = this.buffer.readUInt32LE(this.position);
  271. const high = this.buffer.readUInt32LE(this.position + 4);
  272. this.position += 8;
  273. callback(0x100000000 * high + low);
  274. });
  275. }
  276. readUNumeric96LE(callback) {
  277. this.awaitData(12, () => {
  278. const dword1 = this.buffer.readUInt32LE(this.position);
  279. const dword2 = this.buffer.readUInt32LE(this.position + 4);
  280. const dword3 = this.buffer.readUInt32LE(this.position + 8);
  281. this.position += 12;
  282. callback(dword1 + 0x100000000 * dword2 + 0x100000000 * 0x100000000 * dword3);
  283. });
  284. }
  285. readUNumeric128LE(callback) {
  286. this.awaitData(16, () => {
  287. const dword1 = this.buffer.readUInt32LE(this.position);
  288. const dword2 = this.buffer.readUInt32LE(this.position + 4);
  289. const dword3 = this.buffer.readUInt32LE(this.position + 8);
  290. const dword4 = this.buffer.readUInt32LE(this.position + 12);
  291. this.position += 16;
  292. callback(dword1 + 0x100000000 * dword2 + 0x100000000 * 0x100000000 * dword3 + 0x100000000 * 0x100000000 * 0x100000000 * dword4);
  293. });
  294. } // Variable length data
  295. readBuffer(length, callback) {
  296. this.awaitData(length, () => {
  297. const data = this.buffer.slice(this.position, this.position + length);
  298. this.position += length;
  299. callback(data);
  300. });
  301. } // Read a Unicode String (BVARCHAR)
  302. readBVarChar(callback) {
  303. this.readUInt8(length => {
  304. this.readBuffer(length * 2, data => {
  305. callback(data.toString('ucs2'));
  306. });
  307. });
  308. } // Read a Unicode String (USVARCHAR)
  309. readUsVarChar(callback) {
  310. this.readUInt16LE(length => {
  311. this.readBuffer(length * 2, data => {
  312. callback(data.toString('ucs2'));
  313. });
  314. });
  315. } // Read binary data (BVARBYTE)
  316. readBVarByte(callback) {
  317. this.readUInt8(length => {
  318. this.readBuffer(length, callback);
  319. });
  320. } // Read binary data (USVARBYTE)
  321. readUsVarByte(callback) {
  322. this.readUInt16LE(length => {
  323. this.readBuffer(length, callback);
  324. });
  325. }
  326. }
  327. var _default = Parser;
  328. exports.default = _default;
  329. module.exports = Parser;