drachtio-srf 4.5.20 → 4.5.21

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.
@@ -10,9 +10,32 @@ const DEFAULT_PING_INTERVAL = 15000;
10
10
  const MIN_PING_INTERVAL = 5000;
11
11
  const MAX_PING_INTERVAL = 300000;
12
12
 
13
- function countSymbols(text) {
14
- return [...text].length;
15
- }
13
+ const containsEmoji = (str) => {
14
+ const regex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/;
15
+ return regex.test(str);
16
+ };
17
+
18
+ const countSymbols = (text) => {
19
+ return containsEmoji(text) ? [...text].length : text.length;
20
+ };
21
+
22
+ const parseMessageLengthSpecifier = (str) => {
23
+ for (let i = 0; i < 5; i++) {
24
+ const x = str[i];
25
+ if (i > 0 && x === '#') {
26
+ return {
27
+ numChars: i,
28
+ len: parseInt(str.slice(0, i))
29
+ };
30
+ }
31
+ if (x < '0' || x > '9') return;
32
+ }
33
+ return str[5] === '#' ?
34
+ {
35
+ numChars: 5,
36
+ len: parseInt(str.slice(0, 5))
37
+ } : undefined;
38
+ };
16
39
 
17
40
  module.exports = class WireProtocol extends Emitter {
18
41
 
@@ -297,11 +320,10 @@ module.exports = class WireProtocol extends Emitter {
297
320
  obj.incomingMsg += msg;
298
321
 
299
322
  // check if we have a full message to process
300
- const arr = /^(\d{1,5})#/.exec(obj.incomingMsg);
301
- if (arr) {
302
- const msgLength = parseInt(arr[1]);
303
- if (countSymbols(obj.incomingMsg) >= msgLength + arr[1].length + 1) {
304
- this.processMessageBuffer(socket, obj, msgLength, arr[1].length + 1);
323
+ const {numChars, len} = parseMessageLengthSpecifier(obj.incomingMsg) || {};
324
+ if (len) {
325
+ if (countSymbols(obj.incomingMsg) >= len + numChars + 1) {
326
+ this.processMessageBuffer(socket, obj, len, numChars + 1);
305
327
  }
306
328
  return;
307
329
  }
@@ -309,6 +331,7 @@ module.exports = class WireProtocol extends Emitter {
309
331
  // split in the middle of length specifier
310
332
  return;
311
333
  }
334
+
312
335
  const err = new Error(`invalid message from server, did not start with length specifier: ${obj.incomingMsg}`);
313
336
  if (this.isServer) {
314
337
  console.error(`invalid client message, closing socket: ${err}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drachtio-srf",
3
- "version": "4.5.20",
3
+ "version": "4.5.21",
4
4
  "description": "drachtio signaling resource framework",
5
5
  "main": "lib/srf.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  "jslint": "eslint lib"
11
11
  },
12
12
  "engines": {
13
- "node": ">= 6.9.3"
13
+ "node": ">= 10.x"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
@@ -0,0 +1,33 @@
1
+ const Benchmark = require('benchmark');
2
+ const suite = new Benchmark.Suite();
3
+ const examples = require('sip-message-examples');
4
+ const str = examples('invite');
5
+
6
+ function containsEmoji(str) {
7
+ const regex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/;
8
+ return regex.test(str);
9
+ }
10
+
11
+ const run = async () => {
12
+
13
+ suite
14
+ .add('countSymbols', () => {
15
+ return [...str].length;
16
+ })
17
+ .add('countSymbols after checking for emojis', () => {
18
+ return containsEmoji(str) ? [...str].length : str.length;
19
+ })
20
+ .add('str.length', () => {
21
+ return str.length;
22
+ })
23
+ .on('cycle', function (event) {
24
+ console.log(String(event.target))
25
+ })
26
+ .on('complete', function () {
27
+ console.log('Fastest is ' + this.filter('fastest').map('name'))
28
+ process.exit(0);
29
+ })
30
+ .run({ async: true });
31
+ };
32
+
33
+ run();
@@ -0,0 +1,74 @@
1
+ const exec = require('child_process').exec ;
2
+ const Benchmark = require('benchmark');
3
+ const suite = new Benchmark.Suite();
4
+ const Srf4519 = require('drachtio-srf');
5
+ const srf4519 = new Srf4519();
6
+ const SrfLatest = require('../..');
7
+ const srfLatest = new SrfLatest();
8
+
9
+ const startEnv = async() => {
10
+ return new Promise((resolve, reject) => {
11
+ exec(`docker-compose -f ${__dirname}/../docker-compose-testbed.yaml up -d`, (err) => {
12
+ if (err) return reject(err);
13
+ resolve();
14
+ });
15
+ });
16
+ };
17
+
18
+ const connect = (srf) => {
19
+ return new Promise((resolve, reject) => {
20
+ srf.connect({
21
+ "host": "127.0.0.1",
22
+ "port": 9061,
23
+ "secret": "cymru",
24
+ "enablePing": true,
25
+ "pingInterval": 5000
26
+ });
27
+ srf.on('connect', () => { resolve(); });
28
+ });
29
+ }
30
+
31
+ const run = async () => {
32
+ console.log('starting docker..')
33
+ await startEnv();
34
+ console.log('connecting to drachtio server..')
35
+ await connect(srf4519);
36
+ await connect(srfLatest);
37
+ console.log('starting benchmark..')
38
+
39
+ suite
40
+ .add('drachtio-srf@4.5.19', {
41
+ defer: true,
42
+ fn: (deferred) => {
43
+ srf4519.request('sip:sipp-uas-options', {method: 'OPTIONS'}, (err, req) => {
44
+ if (err) return deferred.reject(err);
45
+ req.on('response', (res) => {
46
+ deferred.resolve();
47
+ });
48
+ });
49
+ }
50
+ })
51
+ /*
52
+ .add('drachtio-srf@latest', {
53
+ defer: true,
54
+ fn: (deferred) => {
55
+ srfLatest.request('sip:sipp-uas-options', {method: 'OPTIONS'}, (err, req) => {
56
+ if (err) return deferred.reject(err);
57
+ req.on('response', (res) => {
58
+ deferred.resolve();
59
+ });
60
+ });
61
+ }
62
+ })
63
+ */
64
+ .on('cycle', function (event) {
65
+ console.log(String(event.target))
66
+ })
67
+ .on('complete', function () {
68
+ console.log('Fastest is ' + this.filter('fastest').map('name'))
69
+ process.exit(0);
70
+ })
71
+ .run({ async: true });
72
+ };
73
+
74
+ run();