generic-transformers.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformRangeReply = exports.pushSlotRangesArguments = exports.pushSortArguments = exports.transformFunctionListItemReply = exports.RedisFunctionFlags = exports.transformCommandReply = exports.CommandCategories = exports.CommandFlags = exports.pushOptionalVerdictArgument = exports.pushVerdictArgument = exports.pushVerdictNumberArguments = exports.pushVerdictArguments = exports.pushEvalArguments = exports.evalFirstKeyIndex = exports.transformPXAT = exports.transformEXAT = exports.transformGeoMembersWithReply = exports.GeoReplyWith = exports.pushGeoRadiusStoreArguments = exports.pushGeoRadiusArguments = exports.pushGeoSearchArguments = exports.pushGeoCountArgument = exports.transformLMPopArguments = exports.transformZMPopArguments = exports.transformSortedSetWithScoresReply = exports.transformSortedSetMemberReply = exports.transformSortedSetMemberNullReply = exports.transformStreamsMessagesReply = exports.transformStreamMessagesNullReply = exports.transformStreamMessagesReply = exports.transformStreamMessageNullReply = exports.transformStreamMessageReply = exports.transformTuplesReply = exports.transformStringNumberInfinityArgument = exports.transformNumberInfinityArgument = exports.transformNumberInfinityNullArrayReply = exports.transformNumberInfinityNullReply = exports.transformNumberInfinityReply = exports.pushScanArguments = exports.transformBooleanArrayReply = exports.transformBooleanReply = void 0;
  4. function transformBooleanReply(reply) {
  5. return reply === 1;
  6. }
  7. exports.transformBooleanReply = transformBooleanReply;
  8. function transformBooleanArrayReply(reply) {
  9. return reply.map(transformBooleanReply);
  10. }
  11. exports.transformBooleanArrayReply = transformBooleanArrayReply;
  12. function pushScanArguments(args, cursor, options) {
  13. args.push(cursor.toString());
  14. if (options?.MATCH) {
  15. args.push('MATCH', options.MATCH);
  16. }
  17. if (options?.COUNT) {
  18. args.push('COUNT', options.COUNT.toString());
  19. }
  20. return args;
  21. }
  22. exports.pushScanArguments = pushScanArguments;
  23. function transformNumberInfinityReply(reply) {
  24. switch (reply.toString()) {
  25. case '+inf':
  26. return Infinity;
  27. case '-inf':
  28. return -Infinity;
  29. default:
  30. return Number(reply);
  31. }
  32. }
  33. exports.transformNumberInfinityReply = transformNumberInfinityReply;
  34. function transformNumberInfinityNullReply(reply) {
  35. if (reply === null)
  36. return null;
  37. return transformNumberInfinityReply(reply);
  38. }
  39. exports.transformNumberInfinityNullReply = transformNumberInfinityNullReply;
  40. function transformNumberInfinityNullArrayReply(reply) {
  41. return reply.map(transformNumberInfinityNullReply);
  42. }
  43. exports.transformNumberInfinityNullArrayReply = transformNumberInfinityNullArrayReply;
  44. function transformNumberInfinityArgument(num) {
  45. switch (num) {
  46. case Infinity:
  47. return '+inf';
  48. case -Infinity:
  49. return '-inf';
  50. default:
  51. return num.toString();
  52. }
  53. }
  54. exports.transformNumberInfinityArgument = transformNumberInfinityArgument;
  55. function transformStringNumberInfinityArgument(num) {
  56. if (typeof num !== 'number')
  57. return num;
  58. return transformNumberInfinityArgument(num);
  59. }
  60. exports.transformStringNumberInfinityArgument = transformStringNumberInfinityArgument;
  61. function transformTuplesReply(reply) {
  62. const message = Object.create(null);
  63. for (let i = 0; i < reply.length; i += 2) {
  64. message[reply[i].toString()] = reply[i + 1];
  65. }
  66. return message;
  67. }
  68. exports.transformTuplesReply = transformTuplesReply;
  69. function transformStreamMessageReply([id, message]) {
  70. return {
  71. id,
  72. message: transformTuplesReply(message)
  73. };
  74. }
  75. exports.transformStreamMessageReply = transformStreamMessageReply;
  76. function transformStreamMessageNullReply(reply) {
  77. if (reply === null)
  78. return null;
  79. return transformStreamMessageReply(reply);
  80. }
  81. exports.transformStreamMessageNullReply = transformStreamMessageNullReply;
  82. function transformStreamMessagesReply(reply) {
  83. return reply.map(transformStreamMessageReply);
  84. }
  85. exports.transformStreamMessagesReply = transformStreamMessagesReply;
  86. function transformStreamMessagesNullReply(reply) {
  87. return reply.map(transformStreamMessageNullReply);
  88. }
  89. exports.transformStreamMessagesNullReply = transformStreamMessagesNullReply;
  90. function transformStreamsMessagesReply(reply) {
  91. if (reply === null)
  92. return null;
  93. return reply.map(([name, rawMessages]) => ({
  94. name,
  95. messages: transformStreamMessagesReply(rawMessages)
  96. }));
  97. }
  98. exports.transformStreamsMessagesReply = transformStreamsMessagesReply;
  99. function transformSortedSetMemberNullReply(reply) {
  100. if (!reply.length)
  101. return null;
  102. return transformSortedSetMemberReply(reply);
  103. }
  104. exports.transformSortedSetMemberNullReply = transformSortedSetMemberNullReply;
  105. function transformSortedSetMemberReply(reply) {
  106. return {
  107. value: reply[0],
  108. score: transformNumberInfinityReply(reply[1])
  109. };
  110. }
  111. exports.transformSortedSetMemberReply = transformSortedSetMemberReply;
  112. function transformSortedSetWithScoresReply(reply) {
  113. const members = [];
  114. for (let i = 0; i < reply.length; i += 2) {
  115. members.push({
  116. value: reply[i],
  117. score: transformNumberInfinityReply(reply[i + 1])
  118. });
  119. }
  120. return members;
  121. }
  122. exports.transformSortedSetWithScoresReply = transformSortedSetWithScoresReply;
  123. function transformZMPopArguments(args, keys, side, options) {
  124. pushVerdictArgument(args, keys);
  125. args.push(side);
  126. if (options?.COUNT) {
  127. args.push('COUNT', options.COUNT.toString());
  128. }
  129. return args;
  130. }
  131. exports.transformZMPopArguments = transformZMPopArguments;
  132. function transformLMPopArguments(args, keys, side, options) {
  133. pushVerdictArgument(args, keys);
  134. args.push(side);
  135. if (options?.COUNT) {
  136. args.push('COUNT', options.COUNT.toString());
  137. }
  138. return args;
  139. }
  140. exports.transformLMPopArguments = transformLMPopArguments;
  141. function pushGeoCountArgument(args, count) {
  142. if (typeof count === 'number') {
  143. args.push('COUNT', count.toString());
  144. }
  145. else if (count) {
  146. args.push('COUNT', count.value.toString());
  147. if (count.ANY) {
  148. args.push('ANY');
  149. }
  150. }
  151. return args;
  152. }
  153. exports.pushGeoCountArgument = pushGeoCountArgument;
  154. function pushGeoSearchArguments(args, key, from, by, options) {
  155. args.push(key);
  156. if (typeof from === 'string') {
  157. args.push('FROMMEMBER', from);
  158. }
  159. else {
  160. args.push('FROMLONLAT', from.longitude.toString(), from.latitude.toString());
  161. }
  162. if ('radius' in by) {
  163. args.push('BYRADIUS', by.radius.toString());
  164. }
  165. else {
  166. args.push('BYBOX', by.width.toString(), by.height.toString());
  167. }
  168. args.push(by.unit);
  169. if (options?.SORT) {
  170. args.push(options.SORT);
  171. }
  172. pushGeoCountArgument(args, options?.COUNT);
  173. return args;
  174. }
  175. exports.pushGeoSearchArguments = pushGeoSearchArguments;
  176. function pushGeoRadiusArguments(args, key, from, radius, unit, options) {
  177. args.push(key);
  178. if (typeof from === 'string') {
  179. args.push(from);
  180. }
  181. else {
  182. args.push(from.longitude.toString(), from.latitude.toString());
  183. }
  184. args.push(radius.toString(), unit);
  185. if (options?.SORT) {
  186. args.push(options.SORT);
  187. }
  188. pushGeoCountArgument(args, options?.COUNT);
  189. return args;
  190. }
  191. exports.pushGeoRadiusArguments = pushGeoRadiusArguments;
  192. function pushGeoRadiusStoreArguments(args, key, from, radius, unit, destination, options) {
  193. pushGeoRadiusArguments(args, key, from, radius, unit, options);
  194. if (options?.STOREDIST) {
  195. args.push('STOREDIST', destination);
  196. }
  197. else {
  198. args.push('STORE', destination);
  199. }
  200. return args;
  201. }
  202. exports.pushGeoRadiusStoreArguments = pushGeoRadiusStoreArguments;
  203. var GeoReplyWith;
  204. (function (GeoReplyWith) {
  205. GeoReplyWith["DISTANCE"] = "WITHDIST";
  206. GeoReplyWith["HASH"] = "WITHHASH";
  207. GeoReplyWith["COORDINATES"] = "WITHCOORD";
  208. })(GeoReplyWith || (exports.GeoReplyWith = GeoReplyWith = {}));
  209. function transformGeoMembersWithReply(reply, replyWith) {
  210. const replyWithSet = new Set(replyWith);
  211. let index = 0;
  212. const distanceIndex = replyWithSet.has(GeoReplyWith.DISTANCE) && ++index, hashIndex = replyWithSet.has(GeoReplyWith.HASH) && ++index, coordinatesIndex = replyWithSet.has(GeoReplyWith.COORDINATES) && ++index;
  213. return reply.map(member => {
  214. const transformedMember = {
  215. member: member[0]
  216. };
  217. if (distanceIndex) {
  218. transformedMember.distance = member[distanceIndex];
  219. }
  220. if (hashIndex) {
  221. transformedMember.hash = member[hashIndex];
  222. }
  223. if (coordinatesIndex) {
  224. const [longitude, latitude] = member[coordinatesIndex];
  225. transformedMember.coordinates = {
  226. longitude,
  227. latitude
  228. };
  229. }
  230. return transformedMember;
  231. });
  232. }
  233. exports.transformGeoMembersWithReply = transformGeoMembersWithReply;
  234. function transformEXAT(EXAT) {
  235. return (typeof EXAT === 'number' ? EXAT : Math.floor(EXAT.getTime() / 1000)).toString();
  236. }
  237. exports.transformEXAT = transformEXAT;
  238. function transformPXAT(PXAT) {
  239. return (typeof PXAT === 'number' ? PXAT : PXAT.getTime()).toString();
  240. }
  241. exports.transformPXAT = transformPXAT;
  242. function evalFirstKeyIndex(options) {
  243. return options?.keys?.[0];
  244. }
  245. exports.evalFirstKeyIndex = evalFirstKeyIndex;
  246. function pushEvalArguments(args, options) {
  247. if (options?.keys) {
  248. args.push(options.keys.length.toString(), ...options.keys);
  249. }
  250. else {
  251. args.push('0');
  252. }
  253. if (options?.arguments) {
  254. args.push(...options.arguments);
  255. }
  256. return args;
  257. }
  258. exports.pushEvalArguments = pushEvalArguments;
  259. function pushVerdictArguments(args, value) {
  260. if (Array.isArray(value)) {
  261. // https://github.com/redis/node-redis/pull/2160
  262. args = args.concat(value);
  263. }
  264. else {
  265. args.push(value);
  266. }
  267. return args;
  268. }
  269. exports.pushVerdictArguments = pushVerdictArguments;
  270. function pushVerdictNumberArguments(args, value) {
  271. if (Array.isArray(value)) {
  272. for (const item of value) {
  273. args.push(item.toString());
  274. }
  275. }
  276. else {
  277. args.push(value.toString());
  278. }
  279. return args;
  280. }
  281. exports.pushVerdictNumberArguments = pushVerdictNumberArguments;
  282. function pushVerdictArgument(args, value) {
  283. if (Array.isArray(value)) {
  284. args.push(value.length.toString(), ...value);
  285. }
  286. else {
  287. args.push('1', value);
  288. }
  289. return args;
  290. }
  291. exports.pushVerdictArgument = pushVerdictArgument;
  292. function pushOptionalVerdictArgument(args, name, value) {
  293. if (value === undefined)
  294. return args;
  295. args.push(name);
  296. return pushVerdictArgument(args, value);
  297. }
  298. exports.pushOptionalVerdictArgument = pushOptionalVerdictArgument;
  299. var CommandFlags;
  300. (function (CommandFlags) {
  301. CommandFlags["WRITE"] = "write";
  302. CommandFlags["READONLY"] = "readonly";
  303. CommandFlags["DENYOOM"] = "denyoom";
  304. CommandFlags["ADMIN"] = "admin";
  305. CommandFlags["PUBSUB"] = "pubsub";
  306. CommandFlags["NOSCRIPT"] = "noscript";
  307. CommandFlags["RANDOM"] = "random";
  308. CommandFlags["SORT_FOR_SCRIPT"] = "sort_for_script";
  309. CommandFlags["LOADING"] = "loading";
  310. CommandFlags["STALE"] = "stale";
  311. CommandFlags["SKIP_MONITOR"] = "skip_monitor";
  312. CommandFlags["ASKING"] = "asking";
  313. CommandFlags["FAST"] = "fast";
  314. CommandFlags["MOVABLEKEYS"] = "movablekeys"; // keys have no pre-determined position. You must discover keys yourself.
  315. })(CommandFlags || (exports.CommandFlags = CommandFlags = {}));
  316. var CommandCategories;
  317. (function (CommandCategories) {
  318. CommandCategories["KEYSPACE"] = "@keyspace";
  319. CommandCategories["READ"] = "@read";
  320. CommandCategories["WRITE"] = "@write";
  321. CommandCategories["SET"] = "@set";
  322. CommandCategories["SORTEDSET"] = "@sortedset";
  323. CommandCategories["LIST"] = "@list";
  324. CommandCategories["HASH"] = "@hash";
  325. CommandCategories["STRING"] = "@string";
  326. CommandCategories["BITMAP"] = "@bitmap";
  327. CommandCategories["HYPERLOGLOG"] = "@hyperloglog";
  328. CommandCategories["GEO"] = "@geo";
  329. CommandCategories["STREAM"] = "@stream";
  330. CommandCategories["PUBSUB"] = "@pubsub";
  331. CommandCategories["ADMIN"] = "@admin";
  332. CommandCategories["FAST"] = "@fast";
  333. CommandCategories["SLOW"] = "@slow";
  334. CommandCategories["BLOCKING"] = "@blocking";
  335. CommandCategories["DANGEROUS"] = "@dangerous";
  336. CommandCategories["CONNECTION"] = "@connection";
  337. CommandCategories["TRANSACTION"] = "@transaction";
  338. CommandCategories["SCRIPTING"] = "@scripting";
  339. })(CommandCategories || (exports.CommandCategories = CommandCategories = {}));
  340. function transformCommandReply([name, arity, flags, firstKeyIndex, lastKeyIndex, step, categories]) {
  341. return {
  342. name,
  343. arity,
  344. flags: new Set(flags),
  345. firstKeyIndex,
  346. lastKeyIndex,
  347. step,
  348. categories: new Set(categories)
  349. };
  350. }
  351. exports.transformCommandReply = transformCommandReply;
  352. var RedisFunctionFlags;
  353. (function (RedisFunctionFlags) {
  354. RedisFunctionFlags["NO_WRITES"] = "no-writes";
  355. RedisFunctionFlags["ALLOW_OOM"] = "allow-oom";
  356. RedisFunctionFlags["ALLOW_STALE"] = "allow-stale";
  357. RedisFunctionFlags["NO_CLUSTER"] = "no-cluster";
  358. })(RedisFunctionFlags || (exports.RedisFunctionFlags = RedisFunctionFlags = {}));
  359. function transformFunctionListItemReply(reply) {
  360. return {
  361. libraryName: reply[1],
  362. engine: reply[3],
  363. functions: reply[5].map(fn => ({
  364. name: fn[1],
  365. description: fn[3],
  366. flags: fn[5]
  367. }))
  368. };
  369. }
  370. exports.transformFunctionListItemReply = transformFunctionListItemReply;
  371. function pushSortArguments(args, options) {
  372. if (options?.BY) {
  373. args.push('BY', options.BY);
  374. }
  375. if (options?.LIMIT) {
  376. args.push('LIMIT', options.LIMIT.offset.toString(), options.LIMIT.count.toString());
  377. }
  378. if (options?.GET) {
  379. for (const pattern of (typeof options.GET === 'string' ? [options.GET] : options.GET)) {
  380. args.push('GET', pattern);
  381. }
  382. }
  383. if (options?.DIRECTION) {
  384. args.push(options.DIRECTION);
  385. }
  386. if (options?.ALPHA) {
  387. args.push('ALPHA');
  388. }
  389. return args;
  390. }
  391. exports.pushSortArguments = pushSortArguments;
  392. function pushSlotRangeArguments(args, range) {
  393. args.push(range.start.toString(), range.end.toString());
  394. }
  395. function pushSlotRangesArguments(args, ranges) {
  396. if (Array.isArray(ranges)) {
  397. for (const range of ranges) {
  398. pushSlotRangeArguments(args, range);
  399. }
  400. }
  401. else {
  402. pushSlotRangeArguments(args, ranges);
  403. }
  404. return args;
  405. }
  406. exports.pushSlotRangesArguments = pushSlotRangesArguments;
  407. function transformRangeReply([start, end]) {
  408. return {
  409. start,
  410. end
  411. };
  412. }
  413. exports.transformRangeReply = transformRangeReply;