@hiroosy/baileys-lite 0.0.1
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 +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* USync (user info sync) query builder + protocols.
|
|
3
|
+
* Combines what used to be 11 files under WAUSync/.
|
|
4
|
+
*/
|
|
5
|
+
import { assertNodeErrorFree, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../binary/wa-binary.js'
|
|
6
|
+
|
|
7
|
+
export class USyncUser {
|
|
8
|
+
withId(id) {
|
|
9
|
+
this.id = id
|
|
10
|
+
return this
|
|
11
|
+
}
|
|
12
|
+
withLid(lid) {
|
|
13
|
+
this.lid = lid
|
|
14
|
+
return this
|
|
15
|
+
}
|
|
16
|
+
withPhone(phone) {
|
|
17
|
+
this.phone = phone
|
|
18
|
+
return this
|
|
19
|
+
}
|
|
20
|
+
withUsername(username) {
|
|
21
|
+
this.username = username
|
|
22
|
+
return this
|
|
23
|
+
}
|
|
24
|
+
withUsernameKey(usernameKey) {
|
|
25
|
+
this.usernameKey = usernameKey
|
|
26
|
+
return this
|
|
27
|
+
}
|
|
28
|
+
withType(type) {
|
|
29
|
+
this.type = type
|
|
30
|
+
return this
|
|
31
|
+
}
|
|
32
|
+
withPersonaId(personaId) {
|
|
33
|
+
this.personaId = personaId
|
|
34
|
+
return this
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class USyncContactProtocol {
|
|
39
|
+
constructor() {
|
|
40
|
+
this.name = 'contact'
|
|
41
|
+
}
|
|
42
|
+
getQueryElement() {
|
|
43
|
+
return { tag: 'contact', attrs: {} }
|
|
44
|
+
}
|
|
45
|
+
getUserElement(user) {
|
|
46
|
+
if (user.phone) return { tag: 'contact', attrs: {}, content: user.phone }
|
|
47
|
+
if (user.username) {
|
|
48
|
+
return {
|
|
49
|
+
tag: 'contact',
|
|
50
|
+
attrs: { username: user.username, ...(user.usernameKey ? { pin: user.usernameKey } : {}), ...(user.lid ? { lid: user.lid } : {}) }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (user.type) return { tag: 'contact', attrs: { type: user.type } }
|
|
54
|
+
return { tag: 'contact', attrs: {} }
|
|
55
|
+
}
|
|
56
|
+
parser(node) {
|
|
57
|
+
if (node.tag === 'contact') {
|
|
58
|
+
assertNodeErrorFree(node)
|
|
59
|
+
return node?.attrs?.type === 'in'
|
|
60
|
+
}
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class USyncDeviceProtocol {
|
|
66
|
+
constructor() {
|
|
67
|
+
this.name = 'devices'
|
|
68
|
+
}
|
|
69
|
+
getQueryElement() {
|
|
70
|
+
return { tag: 'devices', attrs: { version: '2' } }
|
|
71
|
+
}
|
|
72
|
+
getUserElement() {
|
|
73
|
+
// TODO: device phashing, ts, expectedTs; currently returns null (matches upstream)
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
76
|
+
parser(node) {
|
|
77
|
+
const deviceList = []
|
|
78
|
+
let keyIndex
|
|
79
|
+
if (node.tag === 'devices') {
|
|
80
|
+
assertNodeErrorFree(node)
|
|
81
|
+
const deviceListNode = getBinaryNodeChild(node, 'device-list')
|
|
82
|
+
const keyIndexNode = getBinaryNodeChild(node, 'key-index-list')
|
|
83
|
+
if (Array.isArray(deviceListNode?.content)) {
|
|
84
|
+
for (const { tag, attrs } of deviceListNode.content) {
|
|
85
|
+
const id = +attrs.id
|
|
86
|
+
const ki = +attrs['key-index']
|
|
87
|
+
if (tag === 'device') deviceList.push({ id, keyIndex: ki, isHosted: !!(attrs['is_hosted'] && attrs['is_hosted'] === 'true') })
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (keyIndexNode?.tag === 'key-index-list') {
|
|
91
|
+
keyIndex = {
|
|
92
|
+
timestamp: +keyIndexNode.attrs['ts'],
|
|
93
|
+
signedKeyIndex: keyIndexNode?.content,
|
|
94
|
+
expectedTimestamp: keyIndexNode.attrs['expected_ts'] ? +keyIndexNode.attrs['expected_ts'] : undefined
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return { deviceList, keyIndex }
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class USyncDisappearingModeProtocol {
|
|
103
|
+
constructor() {
|
|
104
|
+
this.name = 'disappearing_mode'
|
|
105
|
+
}
|
|
106
|
+
getQueryElement() {
|
|
107
|
+
return { tag: 'disappearing_mode', attrs: {} }
|
|
108
|
+
}
|
|
109
|
+
getUserElement() {
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
parser(node) {
|
|
113
|
+
if (node.tag === 'disappearing_mode') {
|
|
114
|
+
assertNodeErrorFree(node)
|
|
115
|
+
const duration = +node?.attrs.duration
|
|
116
|
+
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
|
|
117
|
+
return { duration, setAt }
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export class USyncStatusProtocol {
|
|
123
|
+
constructor() {
|
|
124
|
+
this.name = 'status'
|
|
125
|
+
}
|
|
126
|
+
getQueryElement() {
|
|
127
|
+
return { tag: 'status', attrs: {} }
|
|
128
|
+
}
|
|
129
|
+
getUserElement() {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
parser(node) {
|
|
133
|
+
if (node.tag === 'status') {
|
|
134
|
+
assertNodeErrorFree(node)
|
|
135
|
+
let status = node?.content?.toString() ?? null
|
|
136
|
+
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
|
|
137
|
+
if (!status) status = node.attrs?.code && +node.attrs.code === 401 ? '' : null
|
|
138
|
+
else if (typeof status === 'string' && status.length === 0) status = null
|
|
139
|
+
return { status, setAt }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class USyncUsernameProtocol {
|
|
145
|
+
constructor() {
|
|
146
|
+
this.name = 'username'
|
|
147
|
+
}
|
|
148
|
+
getQueryElement() {
|
|
149
|
+
return { tag: 'username', attrs: {} }
|
|
150
|
+
}
|
|
151
|
+
getUserElement() {
|
|
152
|
+
return null
|
|
153
|
+
}
|
|
154
|
+
parser(node) {
|
|
155
|
+
if (node.tag === 'username') {
|
|
156
|
+
assertNodeErrorFree(node)
|
|
157
|
+
return typeof node.content === 'string' ? node.content : null
|
|
158
|
+
}
|
|
159
|
+
return null
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export class USyncBotProfileProtocol {
|
|
164
|
+
constructor() {
|
|
165
|
+
this.name = 'bot'
|
|
166
|
+
}
|
|
167
|
+
getQueryElement() {
|
|
168
|
+
return { tag: 'bot', attrs: {}, content: [{ tag: 'profile', attrs: { v: '1' } }] }
|
|
169
|
+
}
|
|
170
|
+
getUserElement(user) {
|
|
171
|
+
return { tag: 'bot', attrs: {}, content: [{ tag: 'profile', attrs: { persona_id: user.personaId } }] }
|
|
172
|
+
}
|
|
173
|
+
parser(node) {
|
|
174
|
+
const botNode = getBinaryNodeChild(node, 'bot')
|
|
175
|
+
const profile = getBinaryNodeChild(botNode, 'profile')
|
|
176
|
+
const commandsNode = getBinaryNodeChild(profile, 'commands')
|
|
177
|
+
const promptsNode = getBinaryNodeChild(profile, 'prompts')
|
|
178
|
+
const commands = []
|
|
179
|
+
const prompts = []
|
|
180
|
+
for (const command of getBinaryNodeChildren(commandsNode, 'command')) {
|
|
181
|
+
commands.push({ name: getBinaryNodeChildString(command, 'name'), description: getBinaryNodeChildString(command, 'description') })
|
|
182
|
+
}
|
|
183
|
+
for (const prompt of getBinaryNodeChildren(promptsNode, 'prompt')) {
|
|
184
|
+
prompts.push(`${getBinaryNodeChildString(prompt, 'emoji')} ${getBinaryNodeChildString(prompt, 'text')}`)
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
isDefault: !!getBinaryNodeChild(profile, 'default'),
|
|
188
|
+
jid: node.attrs.jid,
|
|
189
|
+
name: getBinaryNodeChildString(profile, 'name'),
|
|
190
|
+
attributes: getBinaryNodeChildString(profile, 'attributes'),
|
|
191
|
+
description: getBinaryNodeChildString(profile, 'description'),
|
|
192
|
+
category: getBinaryNodeChildString(profile, 'category'),
|
|
193
|
+
personaId: profile.attrs['persona_id'],
|
|
194
|
+
commandsDescription: getBinaryNodeChildString(commandsNode, 'description'),
|
|
195
|
+
commands,
|
|
196
|
+
prompts
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export class USyncLIDProtocol {
|
|
202
|
+
constructor() {
|
|
203
|
+
this.name = 'lid'
|
|
204
|
+
}
|
|
205
|
+
getQueryElement() {
|
|
206
|
+
return { tag: 'lid', attrs: {} }
|
|
207
|
+
}
|
|
208
|
+
getUserElement(user) {
|
|
209
|
+
return user.lid ? { tag: 'lid', attrs: { jid: user.lid } } : null
|
|
210
|
+
}
|
|
211
|
+
parser(node) {
|
|
212
|
+
return node.tag === 'lid' ? node.attrs.val : null
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export class USyncQuery {
|
|
217
|
+
constructor() {
|
|
218
|
+
this.protocols = []
|
|
219
|
+
this.users = []
|
|
220
|
+
this.context = 'interactive'
|
|
221
|
+
this.mode = 'query'
|
|
222
|
+
}
|
|
223
|
+
withMode(mode) {
|
|
224
|
+
this.mode = mode
|
|
225
|
+
return this
|
|
226
|
+
}
|
|
227
|
+
withContext(context) {
|
|
228
|
+
this.context = context
|
|
229
|
+
return this
|
|
230
|
+
}
|
|
231
|
+
withUser(user) {
|
|
232
|
+
this.users.push(user)
|
|
233
|
+
return this
|
|
234
|
+
}
|
|
235
|
+
parseUSyncQueryResult(result) {
|
|
236
|
+
if (result?.attrs.type !== 'result') return
|
|
237
|
+
const protocolMap = Object.fromEntries(this.protocols.map(protocol => [protocol.name, protocol.parser]))
|
|
238
|
+
const queryResult = { list: [], sideList: [] } // TODO: implement errors etc.
|
|
239
|
+
const usyncNode = getBinaryNodeChild(result, 'usync')
|
|
240
|
+
const listNode = usyncNode ? getBinaryNodeChild(usyncNode, 'list') : undefined
|
|
241
|
+
if (listNode?.content && Array.isArray(listNode.content)) {
|
|
242
|
+
queryResult.list = listNode.content.reduce((acc, node) => {
|
|
243
|
+
const id = node?.attrs.jid
|
|
244
|
+
if (id) {
|
|
245
|
+
const data = Array.isArray(node?.content)
|
|
246
|
+
? Object.fromEntries(
|
|
247
|
+
node.content
|
|
248
|
+
.map(content => {
|
|
249
|
+
const protocol = content.tag
|
|
250
|
+
const parser = protocolMap[protocol]
|
|
251
|
+
return parser ? [protocol, parser(content)] : [protocol, null]
|
|
252
|
+
})
|
|
253
|
+
.filter(([, b]) => b !== null)
|
|
254
|
+
)
|
|
255
|
+
: {}
|
|
256
|
+
acc.push({ ...data, id })
|
|
257
|
+
}
|
|
258
|
+
return acc
|
|
259
|
+
}, [])
|
|
260
|
+
}
|
|
261
|
+
return queryResult
|
|
262
|
+
}
|
|
263
|
+
withDeviceProtocol() {
|
|
264
|
+
this.protocols.push(new USyncDeviceProtocol())
|
|
265
|
+
return this
|
|
266
|
+
}
|
|
267
|
+
withContactProtocol() {
|
|
268
|
+
this.protocols.push(new USyncContactProtocol())
|
|
269
|
+
return this
|
|
270
|
+
}
|
|
271
|
+
withStatusProtocol() {
|
|
272
|
+
this.protocols.push(new USyncStatusProtocol())
|
|
273
|
+
return this
|
|
274
|
+
}
|
|
275
|
+
withDisappearingModeProtocol() {
|
|
276
|
+
this.protocols.push(new USyncDisappearingModeProtocol())
|
|
277
|
+
return this
|
|
278
|
+
}
|
|
279
|
+
withBotProfileProtocol() {
|
|
280
|
+
this.protocols.push(new USyncBotProfileProtocol())
|
|
281
|
+
return this
|
|
282
|
+
}
|
|
283
|
+
withLIDProtocol() {
|
|
284
|
+
this.protocols.push(new USyncLIDProtocol())
|
|
285
|
+
return this
|
|
286
|
+
}
|
|
287
|
+
withUsernameProtocol() {
|
|
288
|
+
this.protocols.push(new USyncUsernameProtocol())
|
|
289
|
+
return this
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events'
|
|
2
|
+
import { DEFAULT_ORIGIN } from '../defaults.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Replaces `ws`, using Node's built-in `WebSocket` (undici-based, Node 22+).
|
|
6
|
+
*
|
|
7
|
+
* Handshake parity with `ws` (verified against a local server that records request headers):
|
|
8
|
+
* - `Origin` -> sent (DEFAULT_ORIGIN), like upstream `ws({ origin })`
|
|
9
|
+
* - `headers` -> config.options.headers, like upstream
|
|
10
|
+
* - `dispatcher` -> config.dispatcher (undici Agent/ProxyAgent) is the native equivalent of
|
|
11
|
+
* `ws`'s `agent`, so proxies work; a `ws`-style `config.agent` is NOT supported.
|
|
12
|
+
* - handshake/connect timeout -> emulated with a timer (native WebSocket has no handshakeTimeout)
|
|
13
|
+
*
|
|
14
|
+
* NOTE: `{ headers, dispatcher }` is an undici extension to the WebSocket constructor, not part of the
|
|
15
|
+
* browser standard. That is fine on Node 22+/24, which is what this package requires.
|
|
16
|
+
*
|
|
17
|
+
* Everything downstream (socket-core.js) talks to this class as a plain Node EventEmitter:
|
|
18
|
+
* `.on('message', buf => ...)`, `.on('open')`, `.on('error')`, `.on('close')`, plus its own namespaced
|
|
19
|
+
* events (`TAG:...`, `CB:...`, `frame`) emitted on itself, not on the underlying transport.
|
|
20
|
+
*/
|
|
21
|
+
export class AbstractSocketClient extends EventEmitter {
|
|
22
|
+
constructor(url, config) {
|
|
23
|
+
super()
|
|
24
|
+
this.url = url
|
|
25
|
+
this.config = config
|
|
26
|
+
this.setMaxListeners(0)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class WebSocketClient extends AbstractSocketClient {
|
|
31
|
+
constructor(...args) {
|
|
32
|
+
super(...args)
|
|
33
|
+
this.socket = null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get isOpen() {
|
|
37
|
+
return this.socket?.readyState === WebSocket.OPEN
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get isClosed() {
|
|
41
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get isClosing() {
|
|
45
|
+
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get isConnecting() {
|
|
49
|
+
return this.socket?.readyState === WebSocket.CONNECTING
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
connect() {
|
|
53
|
+
if (this.socket) return
|
|
54
|
+
|
|
55
|
+
const { options, connectTimeoutMs, dispatcher } = this.config || {}
|
|
56
|
+
const wsOptions = { headers: { Origin: DEFAULT_ORIGIN, ...(options?.headers || {}) } }
|
|
57
|
+
if (dispatcher) wsOptions.dispatcher = dispatcher
|
|
58
|
+
if (this.config?.agent) {
|
|
59
|
+
this.config.logger?.warn?.('`agent` is not supported by the native WebSocket; pass an undici `dispatcher` (e.g. ProxyAgent) instead')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const socket = new WebSocket(this.url, wsOptions)
|
|
63
|
+
this.socket = socket
|
|
64
|
+
socket.binaryType = 'arraybuffer'
|
|
65
|
+
|
|
66
|
+
// emulate ws's `handshakeTimeout`: abort a connection that never opens
|
|
67
|
+
let handshakeTimer
|
|
68
|
+
if (connectTimeoutMs > 0) {
|
|
69
|
+
handshakeTimer = setTimeout(() => {
|
|
70
|
+
if (socket.readyState === WebSocket.CONNECTING) {
|
|
71
|
+
this.emit('error', new Error('Opening handshake has timed out'))
|
|
72
|
+
try { socket.close() } catch {}
|
|
73
|
+
}
|
|
74
|
+
}, connectTimeoutMs)
|
|
75
|
+
handshakeTimer.unref?.()
|
|
76
|
+
}
|
|
77
|
+
const clearHandshake = () => { if (handshakeTimer) { clearTimeout(handshakeTimer); handshakeTimer = undefined } }
|
|
78
|
+
|
|
79
|
+
socket.onopen = () => { clearHandshake(); this.emit('open') }
|
|
80
|
+
socket.onclose = event => { clearHandshake(); this.emit('close', event.code, event.reason) }
|
|
81
|
+
socket.onerror = event => { clearHandshake(); this.emit('error', event.error || new Error(event.message || 'WebSocket error')) }
|
|
82
|
+
socket.onmessage = event => {
|
|
83
|
+
// `ws` handed listeners a Buffer/string; the native WebSocket hands back an ArrayBuffer
|
|
84
|
+
// (binaryType set above) or a string — normalize to match.
|
|
85
|
+
const { data } = event
|
|
86
|
+
this.emit('message', typeof data === 'string' ? data : Buffer.from(data))
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async close() {
|
|
91
|
+
const socket = this.socket
|
|
92
|
+
if (!socket) return
|
|
93
|
+
if (socket.readyState === WebSocket.CLOSED) {
|
|
94
|
+
this.socket = null
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
// connect()'s onclose handler emits 'close'; here we only wait for it (like `ws`'s once('close')).
|
|
98
|
+
// Guarded with a timeout: if the underlying transport is stuck (e.g. a wedged TCP
|
|
99
|
+
// socket), 'close' may never fire and this would otherwise hang teardown forever.
|
|
100
|
+
let onClose
|
|
101
|
+
const closePromise = new Promise(resolve => { onClose = resolve; this.once('close', onClose) })
|
|
102
|
+
try { socket.close() } catch {}
|
|
103
|
+
await Promise.race([closePromise, new Promise(resolve => setTimeout(resolve, 5000))])
|
|
104
|
+
this.off('close', onClose)
|
|
105
|
+
this.socket = null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
send(str, cb) {
|
|
109
|
+
const socket = this.socket
|
|
110
|
+
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
|
111
|
+
cb?.(new Error('WebSocket is not open'))
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
socket.send(str)
|
|
116
|
+
// native send() is fire-and-forget; defer the callback to keep `ws`'s async-callback semantics
|
|
117
|
+
if (cb) queueMicrotask(() => cb())
|
|
118
|
+
} catch (err) {
|
|
119
|
+
cb?.(err)
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
}
|