@gibme/mikrotik 2.0.3 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +51 -13
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -697,24 +697,62 @@ class Mikrotik extends ssh_1.default {
697
697
  terse(command) {
698
698
  return __awaiter(this, void 0, void 0, function* () {
699
699
  const results = [];
700
- const lines = (yield this.exec(command))
701
- .toString()
700
+ (yield this.exec(command)).toString()
702
701
  .split('\r\n')
703
702
  .map(line => line.trim())
704
- .filter(line => line.split(/\s+/).length !== 0 && line.length !== 0);
705
- lines.forEach(line => {
706
- const result = {};
707
- line.split(/\s+/)
708
- .map(col => col.trim())
709
- .forEach(col => {
710
- if (col.includes('=')) {
711
- const [key, ...value] = col.split('=');
712
- result[key] = value.join('=');
703
+ .filter(line => line.split(/\s+/).length !== 0 && line.length !== 0)
704
+ .forEach(line => {
705
+ const result = (() => {
706
+ const [, flags] = line.split(/\s+/).map(part => part.trim());
707
+ const result = {};
708
+ // get the id or the number
709
+ if (line.startsWith('*')) {
710
+ result.id = line.split(/\s/).shift();
711
+ }
712
+ else if (!isNaN(parseInt(line))) {
713
+ result.idx = parseInt(line);
714
+ }
715
+ result.flags = flags.includes('=') ? '' : flags;
716
+ return result;
717
+ })();
718
+ /**
719
+ * This looks like a mess; however, RouterOS will return values with spaced
720
+ * which causes havoc if a simple regex matcher is applied to the key-value pairs
721
+ */
722
+ do {
723
+ const middle = line.indexOf('=') + 1;
724
+ const start = (() => {
725
+ const start = line.substring(0, middle).lastIndexOf(' ');
726
+ return start === -1 ? 0 : start;
727
+ })();
728
+ const next = (() => {
729
+ const next = line.substring(middle).indexOf('=');
730
+ return next === -1 ? -1 : next + middle;
731
+ })();
732
+ const end = (() => {
733
+ if (next === -1)
734
+ return -1;
735
+ const end = line.substring(middle, next).lastIndexOf(' ');
736
+ return end === -1 ? -1 : end + middle;
737
+ })();
738
+ const [key, value] = line.substring(start, end !== -1 ? end : undefined)
739
+ .trim().split(/=/);
740
+ if (parseFloat(value).toString() === value) {
741
+ result[key] = parseFloat(value);
742
+ }
743
+ else if (parseInt(value).toString() === value) {
744
+ result[key] = parseInt(value);
713
745
  }
714
746
  else {
715
- result[col] = col;
747
+ result[key] = value;
716
748
  }
717
- });
749
+ if (end > 0) {
750
+ line = line.substring(end);
751
+ }
752
+ else {
753
+ line = '';
754
+ }
755
+ } while (line.length !== 0);
718
756
  if (Object.keys(result).length !== 0)
719
757
  results.push(result);
720
758
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gibme/mikrotik",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "A simple mikrotik helper/wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",