@oxidezap/baileyrs 0.3.13 → 0.3.14

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.
@@ -28,15 +28,35 @@ export const getServerFromDomainType = (initialServer, domainType) => {
28
28
  export const jidEncode = (user, server, device, agent) => {
29
29
  return `${user || ''}${agent ? `_${agent}` : ''}${device ? `:${device}` : ''}@${server}`;
30
30
  };
31
+ /** Index of `char` in `text[from, limit)`, or -1. */
32
+ const indexBefore = (text, char, from, limit) => {
33
+ const index = text.indexOf(char, from);
34
+ return index < limit ? index : -1;
35
+ };
36
+ /** Where a part that starts at `from` ends: the next `char` before `limit`, else `limit`. */
37
+ const endBefore = (text, char, from, limit) => {
38
+ const index = indexBefore(text, char, from, limit);
39
+ return index < 0 ? limit : index;
40
+ };
31
41
  export const jidDecode = (jid) => {
32
42
  const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1;
33
43
  if (sepIdx < 0) {
34
44
  return undefined;
35
45
  }
36
- const server = jid.slice(sepIdx + 1);
37
- const userCombined = jid.slice(0, sepIdx);
38
- const [userAgent, device] = userCombined.split(':');
39
- const [user, agent] = userAgent.split('_');
46
+ const value = jid;
47
+ const server = value.slice(sepIdx + 1);
48
+ // Same pieces as splitting the pre-`@` half on ':' and then on '_' and
49
+ // keeping the first two parts of each, located by index instead: this runs
50
+ // for every JID on the message pipeline, and the splits allocated two
51
+ // arrays plus a string per part only to read two of them.
52
+ const colonIdx = indexBefore(value, ':', 0, sepIdx);
53
+ const userAgentEnd = colonIdx < 0 ? sepIdx : colonIdx;
54
+ const device = colonIdx < 0 ? undefined : value.slice(colonIdx + 1, endBefore(value, ':', colonIdx + 1, sepIdx));
55
+ const underscoreIdx = indexBefore(value, '_', 0, userAgentEnd);
56
+ const user = value.slice(0, underscoreIdx < 0 ? userAgentEnd : underscoreIdx);
57
+ const agent = underscoreIdx < 0
58
+ ? undefined
59
+ : value.slice(underscoreIdx + 1, endBefore(value, '_', underscoreIdx + 1, userAgentEnd));
40
60
  let domainType = WAJIDDomains.WHATSAPP;
41
61
  if (server === 'lid') {
42
62
  domainType = WAJIDDomains.LID;
@@ -57,8 +77,6 @@ export const jidDecode = (jid) => {
57
77
  device: device ? +device : undefined
58
78
  };
59
79
  };
60
- const CHAR_DEVICE_SEP = 58; // ':'
61
- const CHAR_AGENT_SEP = 95; // '_'
62
80
  /**
63
81
  * Index at which the user component ends, given the index of the '@'.
64
82
  *
@@ -66,15 +84,13 @@ const CHAR_AGENT_SEP = 95; // '_'
66
84
  * '_', so the user always runs to whichever of ':' or '_' comes first, and to
67
85
  * the '@' when neither is there. Locating that boundary answers both callers
68
86
  * below without the intermediate arrays and result object `jidDecode` allocates.
87
+ * Two native searches rather than a per-character loop.
69
88
  */
70
89
  const userEndBefore = (jid, sepIdx) => {
71
- for (let i = 0; i < sepIdx; i++) {
72
- const code = jid.charCodeAt(i);
73
- if (code === CHAR_DEVICE_SEP || code === CHAR_AGENT_SEP) {
74
- return i;
75
- }
76
- }
77
- return sepIdx;
90
+ const colonIdx = indexBefore(jid, ':', 0, sepIdx);
91
+ const end = colonIdx < 0 ? sepIdx : colonIdx;
92
+ const underscoreIdx = indexBefore(jid, '_', 0, end);
93
+ return underscoreIdx < 0 ? end : underscoreIdx;
78
94
  };
79
95
  /** As above, or -1 when the JID has no server part: the case `jidDecode` reports as `undefined`. */
80
96
  const userEnd = (jid) => {
@@ -94,14 +110,10 @@ export const areJidsSameUser = (jid1, jid2) => {
94
110
  if (end < 0) {
95
111
  return true;
96
112
  }
97
- const left = jid1;
113
+ // Both users span `[0, end)`, so they match exactly when one JID starts with
114
+ // the other's user.
98
115
  const right = jid2;
99
- for (let i = 0; i < end; i++) {
100
- if (left.charCodeAt(i) !== right.charCodeAt(i)) {
101
- return false;
102
- }
103
- }
104
- return true;
116
+ return jid1.startsWith(right.slice(0, end));
105
117
  };
106
118
  export const isJidMetaAI = (jid) => jid?.endsWith('@bot');
107
119
  export const isPnUser = (jid) => jid?.endsWith('@s.whatsapp.net');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oxidezap/baileyrs",
3
3
  "type": "module",
4
- "version": "0.3.13",
4
+ "version": "0.3.14",
5
5
  "description": "A Rust-powered WhatsApp Web library for JavaScript, with a Baileys-compatible API",
6
6
  "keywords": [
7
7
  "whatsapp",
@@ -74,10 +74,10 @@
74
74
  "compat:layers": "node scripts/compatibility/check-layer-boundaries.ts",
75
75
  "check:host-boundaries": "node scripts/check-host-boundaries.ts",
76
76
  "example": "node ./Example/example.ts",
77
- "format": "oxfmt src Example",
78
- "format:check": "oxfmt --check src Example",
77
+ "format": "oxfmt src Example bench benchmarks vitest.config.ts",
78
+ "format:check": "oxfmt --check src Example && oxfmt --check bench benchmarks vitest.config.ts",
79
79
  "lint": "tsc && oxlint",
80
- "lint:fix": "oxlint --fix && oxfmt src Example",
80
+ "lint:fix": "oxlint --fix && oxfmt src Example bench benchmarks vitest.config.ts",
81
81
  "prepack": "npm run build && node scripts/check-pack.ts",
82
82
  "prepublishOnly": "node scripts/check-registry-deps.mjs",
83
83
  "prepare": "npm run build",
@@ -90,32 +90,38 @@
90
90
  "test:packaged": "node scripts/packaged-upstream-alias.mjs",
91
91
  "typecheck:compat-auditor": "npm run build --silent && npm run compat:check-waproto --silent && npm run compat:layers --silent && npm run typecheck:compat-declarations --silent",
92
92
  "test:e2e": "NODE_TLS_REJECT_UNAUTHORIZED=0 ADV_SECRET_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= node --expose-gc --test --test-concurrency=1 ./src/__tests__/e2e/*.test-e2e.ts ./scripts/compatibility/__tests__/*.test-e2e.ts",
93
- "typecheck:compat-declarations": "node node_modules/typescript-compat-auditor/bin/tsc -p scripts/compatibility/tsconfig.json"
93
+ "typecheck:compat-declarations": "node node_modules/typescript-compat-auditor/bin/tsc -p scripts/compatibility/tsconfig.json",
94
+ "bench": "vitest bench --run",
95
+ "bench:e2e": "BENCH_SUITE=e2e vitest bench --run",
96
+ "typecheck:bench": "tsc -p bench/tsconfig.json"
94
97
  },
95
98
  "dependencies": {
96
99
  "@oxidezap/whatsapp-rust-bridge": "0.26.4",
97
100
  "events": "^3.3.0",
98
- "fs-native-extensions": "1.5.2",
101
+ "fs-native-extensions": "1.5.3",
99
102
  "long": "^5.3.2"
100
103
  },
101
104
  "devDependencies": {
102
105
  "@bufbuild/protobuf": "^2.13.0",
106
+ "@codspeed/vitest-plugin": "6.1.1",
103
107
  "@hapi/boom": "^10.0.1",
104
108
  "@types/node": "^26.1.1",
105
109
  "baileys": "7.0.0-rc14",
106
110
  "jimp": "^1.6.0",
107
111
  "link-preview-js": "^5.0.0",
108
112
  "music-metadata": "^12.2.2",
109
- "oxfmt": "^0.74.0",
110
- "oxlint": "1.89.0",
113
+ "oxfmt": "^0.77.0",
114
+ "oxlint": "1.92.0",
111
115
  "pino": "^10.3.1",
112
116
  "pino-pretty": "^13.1.3",
113
- "pkg-pr-new": "^0.0.91",
117
+ "pkg-pr-new": "^0.0.93",
114
118
  "protobufjs": "^8.8.0",
115
119
  "tsc-esm-fix": "^3.1.2",
116
120
  "typescript": "^7.0.2",
117
121
  "typescript-compat-auditor": "npm:typescript@6.0.3",
118
- "wrangler": "4.152.0"
122
+ "vite": "8.3.6",
123
+ "vitest": "5.1.0",
124
+ "wrangler": "4.158.0"
119
125
  },
120
126
  "peerDependencies": {
121
127
  "@hapi/boom": "^9.1.0 || ^10.0.0",