@vielhuber/wahelper 1.5.1 → 1.5.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
@@ -37,7 +37,7 @@ export default class wahelperDaemon {
37
37
  this.authFolder = 'auth_' + this.device;
38
38
  this.dbPath = 'whatsapp_' + this.device + '.sqlite';
39
39
  this.logPath = 'whatsapp_' + this.device + '.log';
40
- this.socketPath = 'whatsapp_' + this.device + '.sock';
40
+ this.port = this.computePort(this.device);
41
41
  }
42
42
  }
43
43
 
@@ -88,6 +88,11 @@ export default class wahelperDaemon {
88
88
  return args;
89
89
  }
90
90
 
91
+ computePort(device) {
92
+ // range 29000-31999: below linux ephemeral (32768+) and windows ephemeral (49152+)
93
+ return 29000 + (parseInt(device.slice(-5)) % 3000);
94
+ }
95
+
91
96
  formatNumber(number) {
92
97
  // replace leading zero with 49
93
98
  number = number.replace(/^0+/, '49');
@@ -627,13 +632,8 @@ export default class wahelperDaemon {
627
632
  });
628
633
  });
629
634
 
630
- let socketPath = this.dirname + '/' + this.socketPath;
631
- // remove stale socket file from a previous run
632
- if (fs.existsSync(socketPath)) {
633
- fs.rmSync(socketPath, { force: true });
634
- }
635
- this.httpServer.listen(socketPath, () => {
636
- this.log('HTTP server listening on ' + socketPath);
635
+ this.httpServer.listen(this.port, '127.0.0.1', () => {
636
+ this.log('HTTP server listening on 127.0.0.1:' + this.port);
637
637
  });
638
638
 
639
639
  this.httpServer.on('error', error => {
@@ -686,22 +686,12 @@ export default class wahelperDaemon {
686
686
  this.db.close();
687
687
  this.dbIsOpen = false;
688
688
  }
689
- // clean up socket file
690
- let socketPath = this.dirname + '/' + this.socketPath;
691
- if (fs.existsSync(socketPath)) {
692
- fs.rmSync(socketPath, { force: true });
693
- }
694
689
  this.log('Daemon stopped');
695
690
  }
696
691
 
697
692
  isAlreadyRunning() {
698
693
  return new Promise(resolve => {
699
- let socketPath = this.dirname + '/' + this.socketPath;
700
- if (!fs.existsSync(socketPath)) {
701
- resolve(false);
702
- return;
703
- }
704
- let req = http.request({ socketPath, path: '/status', method: 'GET' }, res => {
694
+ let req = http.request({ host: '127.0.0.1', port: this.port, path: '/status', method: 'GET' }, res => {
705
695
  resolve(res.statusCode === 200);
706
696
  });
707
697
  req.on('error', () => resolve(false));
@@ -732,12 +722,8 @@ export default class wahelperDaemon {
732
722
  process.exit(1);
733
723
  }
734
724
 
735
- this.log(
736
- 'Daemon starting.. (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
737
- );
738
- console.log(
739
- 'Daemon starting... (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
740
- );
725
+ this.log('Daemon starting.. (device: ' + this.device + ', port: 127.0.0.1:' + this.port + ')');
726
+ console.log('Daemon starting... (device: ' + this.device + ', port: 127.0.0.1:' + this.port + ')');
741
727
 
742
728
  this.initDatabase();
743
729
  this.initExitHooks();
package/wahelper.js CHANGED
@@ -24,7 +24,7 @@ export default class wahelper {
24
24
  this.dbPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.sqlite';
25
25
  this.logPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.log';
26
26
  this.dataPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.json';
27
- this.socketPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.sock';
27
+ this.port = this.computePort(this.formatNumber(this.args.device));
28
28
  }
29
29
  }
30
30
 
@@ -220,18 +220,19 @@ export default class wahelper {
220
220
  return { connected: false, message: 'daemon_timeout' };
221
221
  }
222
222
 
223
+ computePort(device) {
224
+ // range 29000-31999: below linux ephemeral (32768+) and windows ephemeral (49152+)
225
+ return 29000 + (parseInt(device.slice(-5)) % 3000);
226
+ }
227
+
223
228
  async callDaemon(method, path, body = null) {
224
229
  return new Promise(resolve => {
225
230
  let postData = body !== null ? JSON.stringify(body) : '';
226
- let options = {
227
- socketPath: this.dirname + '/' + this.socketPath,
228
- path,
229
- method,
230
- headers: {
231
- 'Content-Type': 'application/json',
232
- 'Content-Length': Buffer.byteLength(postData)
233
- }
231
+ let headers = {
232
+ 'Content-Type': 'application/json',
233
+ 'Content-Length': Buffer.byteLength(postData)
234
234
  };
235
+ let options = { host: '127.0.0.1', port: this.port, path, method, headers };
235
236
  let req = http.request(options, res => {
236
237
  let data = '';
237
238
  res.on('data', chunk => {