@hiroosy/baileys-lite 0.0.4 → 0.3.0
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/LICENSE +21 -0
- package/README.md +658 -10
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +702 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -1,22 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Replaces `async-mutex` and `p-queue`.
|
|
3
|
-
*
|
|
4
|
-
* Every usage of both libraries in Baileys is a plain serial (concurrency = 1)
|
|
5
|
-
* task queue: run async tasks one at a time, in submission order, each waiting
|
|
6
|
-
* for the previous to settle. That's implementable with a single chained
|
|
7
|
-
* promise and no external dependency.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/** A single serial execution lane. */
|
|
11
1
|
class SerialQueue {
|
|
12
2
|
constructor() {
|
|
13
3
|
this._tail = Promise.resolve()
|
|
14
4
|
}
|
|
15
5
|
|
|
16
|
-
/** Run `task` once all previously-queued tasks have settled. Returns task's result/throw. */
|
|
17
6
|
run(task) {
|
|
18
7
|
const result = this._tail.then(() => task())
|
|
19
|
-
|
|
8
|
+
|
|
20
9
|
this._tail = result.then(
|
|
21
10
|
() => undefined,
|
|
22
11
|
() => undefined
|
|
@@ -25,7 +14,6 @@ class SerialQueue {
|
|
|
25
14
|
}
|
|
26
15
|
}
|
|
27
16
|
|
|
28
|
-
/** Replacement for async-mutex's `Mutex`: mutex().runExclusive(fn) -> mutex(fn). */
|
|
29
17
|
export const makeMutex = () => {
|
|
30
18
|
const queue = new SerialQueue()
|
|
31
19
|
return {
|
|
@@ -35,7 +23,6 @@ export const makeMutex = () => {
|
|
|
35
23
|
}
|
|
36
24
|
}
|
|
37
25
|
|
|
38
|
-
/** Replacement for a Map<key, Mutex>: one independent serial lane per key, garbage collected when idle. */
|
|
39
26
|
export const makeKeyedMutex = () => {
|
|
40
27
|
const lanes = new Map()
|
|
41
28
|
return {
|
|
@@ -58,10 +45,6 @@ export const makeKeyedMutex = () => {
|
|
|
58
45
|
}
|
|
59
46
|
}
|
|
60
47
|
|
|
61
|
-
/**
|
|
62
|
-
* Replacement for `new PQueue({ concurrency: 1 })`.
|
|
63
|
-
* Only the `.add(fn)` API is used anywhere in Baileys, so that's all this covers.
|
|
64
|
-
*/
|
|
65
48
|
export const makeSerialTaskQueue = () => {
|
|
66
49
|
const queue = new SerialQueue()
|
|
67
50
|
return {
|
|
@@ -71,7 +54,6 @@ export const makeSerialTaskQueue = () => {
|
|
|
71
54
|
}
|
|
72
55
|
}
|
|
73
56
|
|
|
74
|
-
/** Map<key, serial task queue>, created lazily — used by PreKeyManager / auth key queues. */
|
|
75
57
|
export const makeKeyedSerialTaskQueues = () => {
|
|
76
58
|
const queues = new Map()
|
|
77
59
|
return {
|
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native Curve25519 / XEdDSA (Signal-style signatures with X25519 keys).
|
|
3
|
-
*
|
|
4
|
-
* Replaces `curve25519-js` (used indirectly through `libsignal/src/curve.js`).
|
|
5
|
-
* - Key agreement + X25519 keygen use `node:crypto` (native OpenSSL).
|
|
6
|
-
* - XEdDSA sign / verify use BigInt arithmetic over the Ed25519 curve, because
|
|
7
|
-
* Node's Ed25519 API cannot sign with a raw (already-clamped) scalar.
|
|
8
|
-
*
|
|
9
|
-
* Signatures are byte-for-byte identical to `curve25519-js` (deterministic
|
|
10
|
-
* variant, i.e. without the optional 64-byte random suffix) — verified by
|
|
11
|
-
* test/native-deps.mjs against the original package.
|
|
12
|
-
*/
|
|
13
1
|
import { createHash, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, randomBytes } from 'node:crypto'
|
|
14
2
|
|
|
15
3
|
const P = (1n << 255n) - 19n
|
|
16
4
|
const L = (1n << 252n) + 27742317777372353535851937790883648493n
|
|
17
|
-
|
|
5
|
+
|
|
18
6
|
const D = 37095705934669439343138083508754565189542113879843219016388785533085940283555n
|
|
19
7
|
const SQRT_M1 = 19681161376707505956807079304988542015446066515923890162744021073123829784752n
|
|
20
8
|
const BASE_Y = 46316835694926478169428394003475163141307993866256225615783033603165251855960n
|
|
@@ -36,7 +24,6 @@ const pow = (b, e, m = P) => {
|
|
|
36
24
|
}
|
|
37
25
|
const inv = a => pow(a, P - 2n)
|
|
38
26
|
|
|
39
|
-
/* ---- byte <-> bigint (little endian) ---- */
|
|
40
27
|
const bytesToNumLE = b => {
|
|
41
28
|
let n = 0n
|
|
42
29
|
for (let i = b.length - 1; i >= 0; i--) n = (n << 8n) | BigInt(b[i])
|
|
@@ -51,7 +38,6 @@ const numToBytesLE = (n, len) => {
|
|
|
51
38
|
return out
|
|
52
39
|
}
|
|
53
40
|
|
|
54
|
-
/* ---- Edwards curve, extended coordinates (X, Y, Z, T) ---- */
|
|
55
41
|
const ZERO = [0n, 1n, 1n, 0n]
|
|
56
42
|
const BASE = [BASE_X, BASE_Y, 1n, mod(BASE_X * BASE_Y)]
|
|
57
43
|
|
|
@@ -89,7 +75,6 @@ const encodePoint = p => {
|
|
|
89
75
|
return out
|
|
90
76
|
}
|
|
91
77
|
|
|
92
|
-
/** Decode an Edwards point; returns null if it is not on the curve. */
|
|
93
78
|
const decodePoint = bytes => {
|
|
94
79
|
const b = Buffer.from(bytes)
|
|
95
80
|
const sign = (b[31] & 0x80) >> 7
|
|
@@ -99,7 +84,7 @@ const decodePoint = bytes => {
|
|
|
99
84
|
const y2 = mod(y * y)
|
|
100
85
|
const u = mod(y2 - 1n)
|
|
101
86
|
const v = mod(D * y2 + 1n)
|
|
102
|
-
|
|
87
|
+
|
|
103
88
|
const v3 = mod(v * v * v)
|
|
104
89
|
const v7 = mod(v3 * v3 * v)
|
|
105
90
|
let x = mod(u * v3 * pow(mod(u * v7), (P - 5n) / 8n))
|
|
@@ -126,18 +111,15 @@ const clamp = key => {
|
|
|
126
111
|
return k
|
|
127
112
|
}
|
|
128
113
|
|
|
129
|
-
/* ---- DER prefixes for raw X25519 keys (Node crypto) ---- */
|
|
130
114
|
const PUB_DER = Buffer.from('302a300506032b656e032100', 'hex')
|
|
131
115
|
const PRIV_DER = Buffer.from('302e020100300506032b656e04220420', 'hex')
|
|
132
116
|
|
|
133
|
-
/** X25519 shared secret. `pub` = 32 raw bytes, `priv` = 32 raw bytes. */
|
|
134
117
|
export const sharedKey = (priv, pub) =>
|
|
135
118
|
diffieHellman({
|
|
136
119
|
privateKey: createPrivateKey({ key: Buffer.concat([PRIV_DER, priv]), format: 'der', type: 'pkcs8' }),
|
|
137
120
|
publicKey: createPublicKey({ key: Buffer.concat([PUB_DER, pub]), format: 'der', type: 'spki' })
|
|
138
121
|
})
|
|
139
122
|
|
|
140
|
-
/** Random X25519 key pair (32-byte raw keys). */
|
|
141
123
|
export const randomX25519 = () => {
|
|
142
124
|
const { publicKey, privateKey } = generateKeyPairSync('x25519', {
|
|
143
125
|
publicKeyEncoding: { format: 'der', type: 'spki' },
|
|
@@ -149,15 +131,10 @@ export const randomX25519 = () => {
|
|
|
149
131
|
}
|
|
150
132
|
}
|
|
151
133
|
|
|
152
|
-
/**
|
|
153
|
-
* Public key for a given (possibly unclamped) 32-byte private key, exactly like
|
|
154
|
-
* curve25519-js `generateKeyPair(seed).public`: X25519(base point) with the
|
|
155
|
-
* top bit cleared.
|
|
156
|
-
*/
|
|
157
134
|
export const publicFromPrivate = seed => {
|
|
158
135
|
if (seed.length !== 32) throw new Error('wrong seed length')
|
|
159
136
|
const priv = Buffer.from(seed)
|
|
160
|
-
|
|
137
|
+
|
|
161
138
|
const base = Buffer.alloc(32)
|
|
162
139
|
base[0] = 9
|
|
163
140
|
const pub = Buffer.from(sharedKey(priv, base))
|
|
@@ -165,30 +142,25 @@ export const publicFromPrivate = seed => {
|
|
|
165
142
|
return pub
|
|
166
143
|
}
|
|
167
144
|
|
|
168
|
-
/** Curve25519 (Montgomery u) -> Ed25519 (Edwards y): y = (u - 1) / (u + 1) */
|
|
169
145
|
const montToEdY = pk => {
|
|
170
146
|
const b = Buffer.from(pk)
|
|
171
|
-
b[31] &= 0x7f
|
|
147
|
+
b[31] &= 0x7f
|
|
172
148
|
const u = mod(bytesToNumLE(b))
|
|
173
149
|
const y = mod((u - 1n) * inv(mod(u + 1n)))
|
|
174
150
|
return numToBytesLE(y, 32)
|
|
175
151
|
}
|
|
176
152
|
|
|
177
|
-
/**
|
|
178
|
-
* XEdDSA sign (deterministic variant). Equivalent to curve25519-js `sign(sk, msg)`.
|
|
179
|
-
* @returns {Buffer} 64-byte signature
|
|
180
|
-
*/
|
|
181
153
|
export const sign = (secretKey, msg) => {
|
|
182
154
|
if (secretKey.length !== 32) throw new Error('wrong secret key length')
|
|
183
155
|
const a = clamp(secretKey)
|
|
184
156
|
const aNum = bytesToNumLE(a)
|
|
185
157
|
const A = encodePoint(scalarMult(BASE, aNum))
|
|
186
158
|
const signBit = A[31] & 0x80
|
|
187
|
-
|
|
159
|
+
|
|
188
160
|
const r = mod(bytesToNumLE(sha512(a, msg)), L)
|
|
189
161
|
const R = encodePoint(scalarMult(BASE, r))
|
|
190
162
|
const Acompressed = Buffer.from(A)
|
|
191
|
-
|
|
163
|
+
|
|
192
164
|
const h = mod(bytesToNumLE(sha512(R, Acompressed, msg)), L)
|
|
193
165
|
const S = mod(r + h * aNum, L)
|
|
194
166
|
const sig = Buffer.concat([R, numToBytesLE(S, 32)])
|
|
@@ -196,10 +168,6 @@ export const sign = (secretKey, msg) => {
|
|
|
196
168
|
return sig
|
|
197
169
|
}
|
|
198
170
|
|
|
199
|
-
/**
|
|
200
|
-
* XEdDSA verify. Equivalent to curve25519-js `verify(pk, msg, sig)`.
|
|
201
|
-
* @returns {boolean}
|
|
202
|
-
*/
|
|
203
171
|
export const verify = (publicKey, msg, signature) => {
|
|
204
172
|
if (signature.length !== 64) throw new Error('wrong signature length')
|
|
205
173
|
if (publicKey.length !== 32) throw new Error('wrong public key length')
|
|
@@ -212,7 +180,7 @@ export const verify = (publicKey, msg, signature) => {
|
|
|
212
180
|
const R = sig.subarray(0, 32)
|
|
213
181
|
const S = bytesToNumLE(sig.subarray(32))
|
|
214
182
|
const h = mod(bytesToNumLE(sha512(R, edpk, msg)), L)
|
|
215
|
-
|
|
183
|
+
|
|
216
184
|
const negA = [mod(-A[0]), A[1], A[2], mod(-A[3])]
|
|
217
185
|
const check = pointAdd(scalarMult(negA, h), scalarMult(BASE, S))
|
|
218
186
|
return Buffer.compare(encodePoint(check), R) === 0
|
|
@@ -1,26 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native link preview (replaces `link-preview-js`). `fetch` + a small, tolerant HTML head scanner.
|
|
3
|
-
*
|
|
4
|
-
* Result shape follows link-preview-js:
|
|
5
|
-
* HTML page : { url, title, siteName, description, mediaType, contentType, images[], videos[], favicons[] }
|
|
6
|
-
* image : { url, mediaType: 'image', contentType, favicons[] }
|
|
7
|
-
* audio/video/application: { url, mediaType, contentType, favicons[] }
|
|
8
|
-
* Throws Error('... did not receive a valid a url or text') when the text contains no http(s) URL (same message the
|
|
9
|
-
* package used; getUrlInfo relies on it).
|
|
10
|
-
*
|
|
11
|
-
* The HTML scanner reads <meta>, <title>, <link>, <base> and <img> with a state machine that follows the WHATWG HTML
|
|
12
|
-
* tokenizer (quoting, `=` and `/` corner cases, first duplicate attribute wins, unterminated tags dropped, comments,
|
|
13
|
-
* script/style/textarea/title/noscript content treated as text). Named entities: ~100 common ones plus all numeric ones
|
|
14
|
-
* (parse5 knows 2231 named entities; an unknown one is left as written). Verified against cheerio/parse5 in
|
|
15
|
-
* test/native-link-preview.mjs.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
1
|
const REGEX_VALID_URL = /^(?:https?:\/\/)[^\s]+$/i
|
|
19
2
|
const DEFAULT_UA = 'Mozilla/5.0 (compatible; baileys-lite link preview)'
|
|
20
3
|
const MAX_BYTES = 1024 * 1024
|
|
21
4
|
|
|
22
|
-
/* ------------------------------ entities ------------------------------ */
|
|
23
|
-
|
|
24
5
|
const NAMED = {
|
|
25
6
|
amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: '\u00a0', copy: '©', reg: '®', trade: '™', hellip: '…', mdash: '—', ndash: '–',
|
|
26
7
|
lsquo: '‘', rsquo: '’', ldquo: '“', rdquo: '”', laquo: '«', raquo: '»', bull: '•', middot: '·', euro: '€', pound: '£', yen: '¥', cent: '¢',
|
|
@@ -33,14 +14,8 @@ const NAMED = {
|
|
|
33
14
|
Ograve: 'Ò', Oacute: 'Ó', Ocirc: 'Ô', Otilde: 'Õ', Ouml: 'Ö', Oslash: 'Ø', Ugrave: 'Ù', Uacute: 'Ú', Ucirc: 'Û', Uuml: 'Ü', Yacute: 'Ý'
|
|
34
15
|
}
|
|
35
16
|
|
|
36
|
-
// Legacy entities the HTML spec also decodes WITHOUT a trailing semicolon (text context only; see decodeEntities)
|
|
37
17
|
const LEGACY = /^(amp|lt|gt|quot|nbsp|copy|reg|deg|plusmn|times|divide|para|sect|shy|micro|middot|laquo|raquo|iexcl|iquest|cent|pound|yen)$/
|
|
38
18
|
|
|
39
|
-
/**
|
|
40
|
-
* Named entities follow the HTML rules: `&name;` decodes when known; otherwise the LONGEST legacy prefix that is valid
|
|
41
|
-
* without a semicolon decodes (`&8` -> `&8`). Inside an attribute value a semicolon-less match is NOT decoded when the
|
|
42
|
-
* next character is `=` or alphanumeric (so `?a=1©=2` in a URL survives).
|
|
43
|
-
*/
|
|
44
19
|
const decodeEntities = (s, inAttr = false) => {
|
|
45
20
|
if (!s.includes('&')) return s
|
|
46
21
|
return s.replace(/&(?:#(\d+)|#[xX]([0-9a-fA-F]+)|([A-Za-z][A-Za-z0-9]*))(;?)/g, (m, dec, hex, name, semi, offset) => {
|
|
@@ -62,19 +37,10 @@ const decodeEntities = (s, inAttr = false) => {
|
|
|
62
37
|
})
|
|
63
38
|
}
|
|
64
39
|
|
|
65
|
-
/* ------------------------------ HTML scanning ------------------------------ */
|
|
66
|
-
|
|
67
|
-
// Elements whose content is not markup (tokenizer states RCDATA / RAWTEXT / script data). `title` is captured, the rest skipped.
|
|
68
40
|
const TEXT_ELEMENTS = new Set(['script', 'style', 'textarea', 'title', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript'])
|
|
69
41
|
const WS = new Set([' ', '\t', '\n', '\f', '\r'])
|
|
70
42
|
const isAlpha = c => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
|
71
43
|
|
|
72
|
-
/**
|
|
73
|
-
* Follows the WHATWG HTML tokenizer for start tags: attribute names/values, quoting, `=` and `/` corner cases,
|
|
74
|
-
* duplicate attributes (first wins), unterminated tags (dropped), comments, bogus comments (`<!...>`, `<?...>`, CDATA in
|
|
75
|
-
* HTML content) and text-only elements. Returns { metas, links, imgs, title, base } — attribute maps with lower-case
|
|
76
|
-
* names and entity-decoded values.
|
|
77
|
-
*/
|
|
78
44
|
export function scanHtml(html) {
|
|
79
45
|
const out = { metas: [], links: [], imgs: [], title: undefined, base: undefined }
|
|
80
46
|
const n = html.length
|
|
@@ -85,15 +51,15 @@ export function scanHtml(html) {
|
|
|
85
51
|
i = lt + 1
|
|
86
52
|
const c = html[i]
|
|
87
53
|
if (c === '!') {
|
|
88
|
-
|
|
54
|
+
|
|
89
55
|
if (html.startsWith('--', i + 1)) {
|
|
90
|
-
if (html[i + 3] === '>') i += 4
|
|
91
|
-
else if (html.startsWith('->', i + 3)) i += 5
|
|
56
|
+
if (html[i + 3] === '>') i += 4
|
|
57
|
+
else if (html.startsWith('->', i + 3)) i += 5
|
|
92
58
|
else {
|
|
93
59
|
const end = html.indexOf('-->', i + 3)
|
|
94
60
|
const alt = html.indexOf('--!>', i + 3)
|
|
95
61
|
const e = end === -1 ? alt : alt === -1 ? end : Math.min(end, alt)
|
|
96
|
-
if (e === -1) break
|
|
62
|
+
if (e === -1) break
|
|
97
63
|
i = e + (html.startsWith('--!>', e) ? 4 : 3)
|
|
98
64
|
}
|
|
99
65
|
} else {
|
|
@@ -110,7 +76,7 @@ export function scanHtml(html) {
|
|
|
110
76
|
continue
|
|
111
77
|
}
|
|
112
78
|
if (c === '/') {
|
|
113
|
-
|
|
79
|
+
|
|
114
80
|
const d = html[i + 1]
|
|
115
81
|
if (d === '>') {
|
|
116
82
|
i += 2
|
|
@@ -121,24 +87,24 @@ export function scanHtml(html) {
|
|
|
121
87
|
i = gt + 1
|
|
122
88
|
continue
|
|
123
89
|
}
|
|
124
|
-
if (!isAlpha(c)) continue
|
|
125
|
-
|
|
90
|
+
if (!isAlpha(c)) continue
|
|
91
|
+
|
|
126
92
|
let j = i
|
|
127
93
|
while (j < n && !WS.has(html[j]) && html[j] !== '/' && html[j] !== '>') j++
|
|
128
94
|
let name = html.slice(i, j).toLowerCase()
|
|
129
|
-
if (name === 'image') name = 'img'
|
|
95
|
+
if (name === 'image') name = 'img'
|
|
130
96
|
const attrs = {}
|
|
131
97
|
let closed = false
|
|
132
|
-
|
|
98
|
+
|
|
133
99
|
for (;;) {
|
|
134
|
-
while (j < n && (WS.has(html[j]) || html[j] === '/')) j++
|
|
135
|
-
if (j >= n) break
|
|
100
|
+
while (j < n && (WS.has(html[j]) || html[j] === '/')) j++
|
|
101
|
+
if (j >= n) break
|
|
136
102
|
if (html[j] === '>') {
|
|
137
103
|
j++
|
|
138
104
|
closed = true
|
|
139
105
|
break
|
|
140
106
|
}
|
|
141
|
-
|
|
107
|
+
|
|
142
108
|
let k = j + 1
|
|
143
109
|
while (k < n && !WS.has(html[k]) && html[k] !== '/' && html[k] !== '>' && html[k] !== '=') k++
|
|
144
110
|
const aname = html.slice(j, k).toLowerCase().replace(/\0/g, '\ufffd')
|
|
@@ -152,7 +118,7 @@ export function scanHtml(html) {
|
|
|
152
118
|
if (q === '"' || q === "'") {
|
|
153
119
|
const e = html.indexOf(q, j + 1)
|
|
154
120
|
if (e === -1) {
|
|
155
|
-
j = n
|
|
121
|
+
j = n
|
|
156
122
|
break
|
|
157
123
|
}
|
|
158
124
|
value = html.slice(j + 1, e)
|
|
@@ -165,7 +131,7 @@ export function scanHtml(html) {
|
|
|
165
131
|
}
|
|
166
132
|
value = decodeEntities(value, true)
|
|
167
133
|
}
|
|
168
|
-
if (!(aname in attrs)) attrs[aname] = value
|
|
134
|
+
if (!(aname in attrs)) attrs[aname] = value
|
|
169
135
|
}
|
|
170
136
|
if (!closed) break
|
|
171
137
|
i = j
|
|
@@ -176,7 +142,7 @@ export function scanHtml(html) {
|
|
|
176
142
|
if (out.base === undefined && attrs.href) out.base = attrs.href
|
|
177
143
|
}
|
|
178
144
|
if (TEXT_ELEMENTS.has(name)) {
|
|
179
|
-
|
|
145
|
+
|
|
180
146
|
const re = new RegExp('</' + name + '(?=[\\s/>])', 'i')
|
|
181
147
|
const m = re.exec(html.slice(i))
|
|
182
148
|
const end = m ? i + m.index : n
|
|
@@ -195,8 +161,6 @@ const resolveUrl = (href, base) => {
|
|
|
195
161
|
}
|
|
196
162
|
}
|
|
197
163
|
|
|
198
|
-
/* ------------------------------ fetching ------------------------------ */
|
|
199
|
-
|
|
200
164
|
const charsetFrom = (contentType, head) => {
|
|
201
165
|
const h = /charset\s*=\s*["']?([\w.:-]+)/i.exec(contentType || '')
|
|
202
166
|
if (h) return h[1]
|
|
@@ -214,7 +178,6 @@ const decodeBody = (bytes, contentType) => {
|
|
|
214
178
|
}
|
|
215
179
|
}
|
|
216
180
|
|
|
217
|
-
/** Reads at most `max` bytes; stops early once `</head>` has been seen (everything we need is there). */
|
|
218
181
|
const readBody = async (res, max) => {
|
|
219
182
|
const reader = res.body?.getReader()
|
|
220
183
|
if (!reader) return new Uint8Array(await res.arrayBuffer()).subarray(0, max)
|
|
@@ -240,11 +203,6 @@ const readBody = async (res, max) => {
|
|
|
240
203
|
return buf
|
|
241
204
|
}
|
|
242
205
|
|
|
243
|
-
/**
|
|
244
|
-
* @param {string} text a URL, or text containing one
|
|
245
|
-
* @param {{ timeout?: number, headers?: Record<string,string>, followRedirects?: 'follow'|'manual'|'error',
|
|
246
|
-
* handleRedirects?: (baseUrl: string, forwardedUrl: string) => boolean, maxBytes?: number }} [options]
|
|
247
|
-
*/
|
|
248
206
|
export async function getLinkPreview(text, options = {}) {
|
|
249
207
|
if (typeof text !== 'string') throw new Error('link-preview did not receive a valid a url or text')
|
|
250
208
|
const detected = text.replace(/\n/g, ' ').split(' ').find(token => REGEX_VALID_URL.test(token))
|
package/src/foundation/logger.js
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal drop-in logger replacing `pino`.
|
|
3
|
-
* Supports the subset of the pino API Baileys actually uses:
|
|
4
|
-
* logger.info/warn/error/debug/trace/fatal(obj?, msg?)
|
|
5
|
-
* logger.child(bindings) -> returns a new logger merging bindings into every line
|
|
6
|
-
* logger.level (get/set)
|
|
7
|
-
*
|
|
8
|
-
* Output: newline-delimited JSON on stdout/stderr, matching pino's default
|
|
9
|
-
* shape closely enough for log processors that expect { level, time, msg }.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
1
|
const LEVELS = { trace: 10, debug: 20, info: 30, warn: 40, error: 50, fatal: 60 }
|
|
13
2
|
|
|
14
3
|
function serializeError(err) {
|
package/src/foundation/long.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal 64-bit integer, API-compatible with the subset of `long` (long.js 5.x)
|
|
3
|
-
* that protobufjs and WAProto actually use. Replaces the `long` package.
|
|
4
|
-
*
|
|
5
|
-
* Shape is intentionally the same as long.js so consumers keep working:
|
|
6
|
-
* { low, high, unsigned } (low/high are signed 32-bit ints)
|
|
7
|
-
* toNumber(), toString(), toJSON-free, isLong via __isLong__
|
|
8
|
-
* Arithmetic is done with BigInt instead of long.js's emulated 32-bit math.
|
|
9
|
-
*/
|
|
10
1
|
const TWO_64 = 1n << 64n
|
|
11
2
|
const MASK64 = TWO_64 - 1n
|
|
12
3
|
|
|
@@ -19,11 +10,10 @@ export class Long {
|
|
|
19
10
|
this.unsigned = !!unsigned
|
|
20
11
|
}
|
|
21
12
|
|
|
22
|
-
/** Underlying 64 bits as an unsigned BigInt. */
|
|
23
13
|
_bits() {
|
|
24
14
|
return (BigInt(this.high >>> 0) << 32n) | BigInt(this.low >>> 0)
|
|
25
15
|
}
|
|
26
|
-
|
|
16
|
+
|
|
27
17
|
toBigInt() {
|
|
28
18
|
const b = this._bits()
|
|
29
19
|
return this.unsigned ? b : BigInt.asIntN(64, b)
|
|
@@ -84,7 +74,7 @@ export class Long {
|
|
|
84
74
|
if (Number.isNaN(value)) return new Long(0, 0, unsigned)
|
|
85
75
|
if (unsigned) {
|
|
86
76
|
if (value < 0) return new Long(0, 0, true)
|
|
87
|
-
if (value >= 18446744073709551616) return new Long(-1, -1, true)
|
|
77
|
+
if (value >= 18446744073709551616) return new Long(-1, -1, true)
|
|
88
78
|
} else {
|
|
89
79
|
if (value <= -9223372036854775808) return new Long(0, -2147483648, false)
|
|
90
80
|
if (value + 1 >= 9223372036854775808) return new Long(-1, 2147483647, false)
|
|
@@ -107,7 +97,7 @@ export class Long {
|
|
|
107
97
|
if (p > 0) throw Error('interior hyphen')
|
|
108
98
|
const neg = p === 0
|
|
109
99
|
const digits = neg ? str.substring(1) : str
|
|
110
|
-
|
|
100
|
+
|
|
111
101
|
let result = 0n
|
|
112
102
|
const R = BigInt(radix)
|
|
113
103
|
for (let i = 0; i < digits.length; i += 8) {
|
|
@@ -123,7 +113,7 @@ export class Long {
|
|
|
123
113
|
if (typeof val === 'number') return Long.fromNumber(val, unsigned)
|
|
124
114
|
if (typeof val === 'string') return Long.fromString(val, unsigned)
|
|
125
115
|
if (typeof val === 'bigint') return Long.fromBigInt(val, unsigned)
|
|
126
|
-
|
|
116
|
+
|
|
127
117
|
return new Long(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned)
|
|
128
118
|
}
|
|
129
119
|
|
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native replacement for `protobufjs/minimal` (+ @protobufjs/* + long), Node-only.
|
|
3
|
-
*
|
|
4
|
-
* Provides exactly the surface the generated WAProto/index.js consumes:
|
|
5
|
-
* Reader (BufferReader semantics), Writer (BufferWriter semantics), util, roots.
|
|
6
|
-
* Wire output and decode results are byte/shape-identical to protobufjs 7.5.x —
|
|
7
|
-
* verified by test/native-protobuf.mjs (differential + fuzz against the original).
|
|
8
|
-
*
|
|
9
|
-
* Intentionally NOT supported (unused by WAProto, browser-only in protobufjs):
|
|
10
|
-
* plain-Array buffers, Uint8Array pool, rpc, reflection, eventemitter, float polyfill.
|
|
11
|
-
*/
|
|
12
1
|
import { Long } from './long.js'
|
|
13
2
|
|
|
14
|
-
/* ------------------------------------------------------------------ */
|
|
15
|
-
/* LongBits (lo/hi as unsigned 32-bit) — port of protobufjs util/longbits */
|
|
16
|
-
/* ------------------------------------------------------------------ */
|
|
17
|
-
|
|
18
3
|
export class LongBits {
|
|
19
4
|
constructor(lo, hi) {
|
|
20
5
|
this.lo = lo >>> 0
|
|
@@ -91,22 +76,18 @@ export class LongBits {
|
|
|
91
76
|
}
|
|
92
77
|
}
|
|
93
78
|
LongBits.zero = new LongBits(0, 0)
|
|
94
|
-
|
|
79
|
+
|
|
95
80
|
LongBits.zero.toNumber = () => 0
|
|
96
81
|
LongBits.zero.zzEncode = LongBits.zero.zzDecode = function () {
|
|
97
82
|
return this
|
|
98
83
|
}
|
|
99
84
|
LongBits.zero.length = () => 1
|
|
100
85
|
|
|
101
|
-
/* ------------------------------------------------------------------ */
|
|
102
|
-
/* Reader */
|
|
103
|
-
/* ------------------------------------------------------------------ */
|
|
104
|
-
|
|
105
86
|
const indexOutOfRange = (reader, writeLength) =>
|
|
106
87
|
RangeError('index out of range: ' + reader.pos + ' + ' + (writeLength || 1) + ' > ' + reader.len)
|
|
107
88
|
|
|
108
89
|
function readVarint32NearEnd(reader) {
|
|
109
|
-
|
|
90
|
+
|
|
110
91
|
let value = 0
|
|
111
92
|
for (let i = 0; i < 4; ++i) {
|
|
112
93
|
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
@@ -121,12 +102,12 @@ function readLongVarint(reader) {
|
|
|
121
102
|
const bits = new LongBits(0, 0)
|
|
122
103
|
let i = 0
|
|
123
104
|
if (reader.len - reader.pos > 4) {
|
|
124
|
-
|
|
105
|
+
|
|
125
106
|
for (; i < 4; ++i) {
|
|
126
107
|
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
127
108
|
if (reader.buf[reader.pos++] < 128) return bits
|
|
128
109
|
}
|
|
129
|
-
|
|
110
|
+
|
|
130
111
|
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << 28)) >>> 0
|
|
131
112
|
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) >> 4)) >>> 0
|
|
132
113
|
if (reader.buf[reader.pos++] < 128) return bits
|
|
@@ -137,12 +118,12 @@ function readLongVarint(reader) {
|
|
|
137
118
|
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
138
119
|
if (reader.buf[reader.pos++] < 128) return bits
|
|
139
120
|
}
|
|
140
|
-
|
|
121
|
+
|
|
141
122
|
bits.lo = (bits.lo | ((reader.buf[reader.pos++] & 127) << (i * 7))) >>> 0
|
|
142
123
|
return bits
|
|
143
124
|
}
|
|
144
125
|
if (reader.len - reader.pos > 4) {
|
|
145
|
-
|
|
126
|
+
|
|
146
127
|
for (; i < 5; ++i) {
|
|
147
128
|
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) << (i * 7 + 3))) >>> 0
|
|
148
129
|
if (reader.buf[reader.pos++] < 128) return bits
|
|
@@ -166,7 +147,6 @@ export class Reader {
|
|
|
166
147
|
this.len = buffer.length
|
|
167
148
|
}
|
|
168
149
|
|
|
169
|
-
/** @returns {Reader} BufferReader-equivalent for Buffers; accepts Uint8Array too (like protobufjs) */
|
|
170
150
|
static create(buffer) {
|
|
171
151
|
if (Buffer.isBuffer(buffer) || buffer instanceof Uint8Array || Array.isArray(buffer)) return new Reader(buffer)
|
|
172
152
|
throw Error('illegal buffer')
|
|
@@ -250,13 +230,13 @@ export class Reader {
|
|
|
250
230
|
this.pos += length
|
|
251
231
|
if (Array.isArray(this.buf)) return this.buf.slice(start, end)
|
|
252
232
|
if (start === end) return Buffer.alloc(0)
|
|
253
|
-
|
|
233
|
+
|
|
254
234
|
return Buffer.isBuffer(this.buf) ? this.buf.slice(start, end) : this.buf.subarray(start, end)
|
|
255
235
|
}
|
|
256
236
|
string() {
|
|
257
237
|
if (Buffer.isBuffer(this.buf)) {
|
|
258
|
-
const len = this.uint32()
|
|
259
|
-
|
|
238
|
+
const len = this.uint32()
|
|
239
|
+
|
|
260
240
|
return this.buf.utf8Slice(this.pos, (this.pos = Math.min(this.pos + len, this.len)))
|
|
261
241
|
}
|
|
262
242
|
const b = this.bytes()
|
|
@@ -298,11 +278,7 @@ export class Reader {
|
|
|
298
278
|
return this
|
|
299
279
|
}
|
|
300
280
|
}
|
|
301
|
-
Reader.recursionLimit = 100
|
|
302
|
-
|
|
303
|
-
/* ------------------------------------------------------------------ */
|
|
304
|
-
/* Writer (linked list of ops, like protobufjs; needed for fork/ldelim) */
|
|
305
|
-
/* ------------------------------------------------------------------ */
|
|
281
|
+
Reader.recursionLimit = 100
|
|
306
282
|
|
|
307
283
|
class Op {
|
|
308
284
|
constructor(fn, len, val) {
|
|
@@ -360,7 +336,7 @@ const writeDouble = (val, buf, pos) => {
|
|
|
360
336
|
Buffer.prototype.writeDoubleLE.call(buf, val, pos)
|
|
361
337
|
}
|
|
362
338
|
const writeBytes = (val, buf, pos) => {
|
|
363
|
-
buf.set(val, pos)
|
|
339
|
+
buf.set(val, pos)
|
|
364
340
|
}
|
|
365
341
|
const writeString = (val, buf, pos) => {
|
|
366
342
|
buf.utf8Write(val, pos)
|
|
@@ -390,7 +366,7 @@ export class Writer {
|
|
|
390
366
|
}
|
|
391
367
|
int32(value) {
|
|
392
368
|
return (value |= 0) < 0
|
|
393
|
-
? this._push(writeVarint64, 10, LongBits.fromNumber(value))
|
|
369
|
+
? this._push(writeVarint64, 10, LongBits.fromNumber(value))
|
|
394
370
|
: this.uint32(value)
|
|
395
371
|
}
|
|
396
372
|
sint32(value) {
|
|
@@ -430,7 +406,7 @@ export class Writer {
|
|
|
430
406
|
return this._push(writeDouble, 8, value)
|
|
431
407
|
}
|
|
432
408
|
bytes(value) {
|
|
433
|
-
|
|
409
|
+
|
|
434
410
|
if (typeof value === 'string' || value instanceof String) value = Buffer.from(value, 'base64')
|
|
435
411
|
const len = value.length >>> 0
|
|
436
412
|
this.uint32(len)
|
|
@@ -467,14 +443,14 @@ export class Writer {
|
|
|
467
443
|
const len = this.len
|
|
468
444
|
this.reset().uint32(len)
|
|
469
445
|
if (len) {
|
|
470
|
-
this.tail.next = head.next
|
|
446
|
+
this.tail.next = head.next
|
|
471
447
|
this.tail = tail
|
|
472
448
|
this.len += len
|
|
473
449
|
}
|
|
474
450
|
return this
|
|
475
451
|
}
|
|
476
452
|
finish() {
|
|
477
|
-
let head = this.head.next
|
|
453
|
+
let head = this.head.next
|
|
478
454
|
const buf = Writer.alloc(this.len)
|
|
479
455
|
let pos = 0
|
|
480
456
|
while (head) {
|
|
@@ -486,11 +462,6 @@ export class Writer {
|
|
|
486
462
|
}
|
|
487
463
|
}
|
|
488
464
|
|
|
489
|
-
/* ------------------------------------------------------------------ */
|
|
490
|
-
/* util */
|
|
491
|
-
/* ------------------------------------------------------------------ */
|
|
492
|
-
|
|
493
|
-
// base64 tables (same construction as @protobufjs/base64): B64 = index -> char code, S64 = char code -> index (sparse)
|
|
494
465
|
const B64 = new Array(64)
|
|
495
466
|
const S64 = new Array(123)
|
|
496
467
|
for (let i = 0; i < 64; ) S64[(B64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : (i - 59) | 43)] = i++
|
|
@@ -508,7 +479,7 @@ export const util = {
|
|
|
508
479
|
toJSONOptions: { longs: String, enums: String, bytes: String, json: true },
|
|
509
480
|
newBuffer: sizeOrArray => (typeof sizeOrArray === 'number' ? Buffer.allocUnsafe(sizeOrArray) : Buffer.from(sizeOrArray)),
|
|
510
481
|
base64: {
|
|
511
|
-
|
|
482
|
+
|
|
512
483
|
length(string) {
|
|
513
484
|
let p = string.length
|
|
514
485
|
if (!p) return 0
|
|
@@ -516,13 +487,13 @@ export const util = {
|
|
|
516
487
|
while (--p % 4 > 1 && string.charAt(p) === '=') ++n
|
|
517
488
|
return Math.ceil(string.length * 3) / 4 - n
|
|
518
489
|
},
|
|
519
|
-
|
|
490
|
+
|
|
520
491
|
encode(buffer, start, end) {
|
|
521
492
|
let parts = null
|
|
522
493
|
let chunk = []
|
|
523
|
-
let i = 0
|
|
524
|
-
let j = 0
|
|
525
|
-
let t
|
|
494
|
+
let i = 0
|
|
495
|
+
let j = 0
|
|
496
|
+
let t
|
|
526
497
|
while (start < end) {
|
|
527
498
|
const b = buffer[start++]
|
|
528
499
|
switch (j) {
|
|
@@ -558,10 +529,7 @@ export const util = {
|
|
|
558
529
|
}
|
|
559
530
|
return String.fromCharCode.apply(String, chunk.slice(0, i))
|
|
560
531
|
},
|
|
561
|
-
|
|
562
|
-
* Strict standard-alphabet decode. Throws 'invalid encoding' on any character outside
|
|
563
|
-
* A-Za-z0-9+/ (so URL-safe '-'/'_', whitespace etc. are REJECTED — unlike Buffer.from(...,'base64')).
|
|
564
|
-
*/
|
|
532
|
+
|
|
565
533
|
decode(string, buffer, offset) {
|
|
566
534
|
const start = offset
|
|
567
535
|
let j = 0
|