@gibme/mikrotik 2.0.4 → 2.0.6

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 +50 -15
  2. package/package.json +5 -5
package/dist/index.js CHANGED
@@ -697,27 +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 (!result.idx && !isNaN(parseInt(col))) {
711
- result.idx = parseInt(col);
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);
712
714
  }
713
- if (col.includes('=')) {
714
- const [key, ...value] = col.split('=');
715
- result[key] = value.join('=');
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);
716
745
  }
717
746
  else {
718
- result[col] = col;
747
+ result[key] = value;
719
748
  }
720
- });
749
+ if (end > 0) {
750
+ line = line.substring(end);
751
+ }
752
+ else {
753
+ line = '';
754
+ }
755
+ } while (line.length !== 0);
721
756
  if (Object.keys(result).length !== 0)
722
757
  results.push(result);
723
758
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gibme/mikrotik",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "A simple mikrotik helper/wrapper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -55,10 +55,10 @@
55
55
  "typescript": "^5.3.3"
56
56
  },
57
57
  "dependencies": {
58
- "@gibme/cache": "^1.1.5",
59
- "@gibme/ssh": "^2.0.0",
58
+ "@gibme/cache": "^1.1.13",
59
+ "@gibme/ssh": "^2.0.1",
60
60
  "@types/jsbn": "^1.2.33",
61
- "ip-address": "^9.0.5",
62
- "semver": "^7.6.2"
61
+ "ip-address": "^10.0.1",
62
+ "semver": "^7.7.1"
63
63
  }
64
64
  }