@vielhuber/wahelper 1.5.1 → 1.5.3

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/README.MD CHANGED
@@ -106,24 +106,28 @@ $wahelper->sendGroup(
106
106
  # install pm2
107
107
  npm install -g pm2
108
108
 
109
- # start daemon
109
+ # start daemon (linux)
110
110
  pm2 start npx --name wahelper-xxxxxxxxxxxx --cwd /var/www/wahelper -- wahelper-daemon --device xxxxxxxxxxxx
111
111
 
112
- # optional: autostart
113
- ## save config
112
+ # start daemon (windows)
113
+ pm2 start node --name wahelper-xxxxxxxxxxxx --cwd "C:\path\to\project" -- node_modules/@vielhuber/wahelper/wahelper-daemon.js --device xxxxxxxxxxxx
114
+
115
+ ## autostart (linux)
114
116
  pm2 save
115
- ## linux
116
117
  pm2 startup
117
- ## windows
118
- Windows Task Scheduler > Trigger Bei Systemstart → Aktion "pm2 resurrect"
118
+
119
+ ## autostart (windows)
120
+ pm2 save
121
+ Windows Task Scheduler > Create Task > Triggers: At startup → Actions: Start a program → Program: `C:\Users\<user>\AppData\Roaming\npm\pm2.cmd`, Arguments: `resurrect`
119
122
 
120
123
  # more commands
121
- pm2 resurrect
122
- pm2 unstartup
123
- pm2 status
124
- pm2 start wahelper-xxxxxxxxxxxx
125
- pm2 stop wahelper-xxxxxxxxxxxx
126
- pm2 restart wahelper-xxxxxxxxxxxx
127
- pm2 logs wahelper-xxxxxxxxxxxx
128
- pm2 delete wahelper-xxxxxxxxxxxx
124
+ pm2 unstartup # remove autostart
125
+ pm2 status # show status of all processes
126
+ pm2 resurrect # restore saved process list
127
+ pm2 save # save current process list for resurrect
128
+ pm2 start wahelper-xxxxxxxxxxxx # start a stopped process
129
+ pm2 stop wahelper-xxxxxxxxxxxx # stop a running process
130
+ pm2 restart wahelper-xxxxxxxxxxxx # restart a process
131
+ pm2 logs wahelper-xxxxxxxxxxxx # tail live logs
132
+ pm2 delete wahelper-xxxxxxxxxxxx # remove process
129
133
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.5.1",
3
+ "version": "1.5.3",
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 => {