@itznomo/baileys 0.0.0-stage → 0.0.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/BUTTONS.md +153 -0
- package/Example/call-bot.mjs +38 -0
- package/Example/example.mjs +36 -0
- package/Example/full-bot-commands.mjs +319 -0
- package/Example/full-bot.mjs +77 -0
- package/LICENSE +21 -0
- package/README.md +543 -2
- package/WAProto/@dnuzi +1 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +113300 -0
- package/assets/banner.svg +17 -0
- package/assets/devider.svg +1 -0
- package/assets/features.svg +21 -0
- package/docs/CALLS.md +106 -0
- package/docs/FEATURES.md +1152 -0
- package/engine-requirements.js +19 -0
- package/index.d.ts +815 -0
- package/package.json +51 -4
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +627 -0
- package/src/constants.js +147 -0
- package/src/defaults.js +106 -0
- package/src/foundation/ai-rich.js +654 -0
- package/src/foundation/audio-duration.js +470 -0
- package/src/foundation/audio-samples.js +250 -0
- package/src/foundation/boom.js +39 -0
- package/src/foundation/builders.js +851 -0
- package/src/foundation/cache.js +134 -0
- package/src/foundation/concurrency.js +69 -0
- package/src/foundation/curve25519.js +189 -0
- package/src/foundation/image-thumb.js +65 -0
- package/src/foundation/link-preview.js +278 -0
- package/src/foundation/logger.js +78 -0
- package/src/foundation/long.js +130 -0
- package/src/foundation/performance.js +385 -0
- package/src/foundation/protobuf-minimal.js +587 -0
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +47 -0
- package/src/foundation/zip.js +108 -0
- package/src/index.js +67 -0
- package/src/signal/libsignal.js +996 -0
- package/src/signal/signal-group.js +455 -0
- package/src/signal/signal-repository.js +635 -0
- package/src/socket/business-communities.js +811 -0
- package/src/socket/chats.js +702 -0
- package/src/socket/groups-newsletter.js +524 -0
- package/src/socket/messages-recv.js +1815 -0
- package/src/socket/messages-send.js +1526 -0
- package/src/socket/socket-builder.js +131 -0
- package/src/socket/socket-core.js +913 -0
- package/src/socket/usync.js +287 -0
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +170 -0
- package/src/utils/auth-state-core.js +731 -0
- package/src/utils/auth-state-storage.js +594 -0
- package/src/utils/chat-sync.js +1046 -0
- package/src/utils/check-status.js +159 -0
- package/src/utils/cstoken.js +17 -0
- package/src/utils/media.js +807 -0
- package/src/utils/message-compose.js +1747 -0
- package/src/utils/message-processing.js +912 -0
- package/src/utils/modern-messages.js +435 -0
- package/src/utils/nomo-buttons.js +138 -0
- package/src/utils/nomo-commands.js +168 -0
- package/src/utils/spam-report-utils.js +136 -0
- package/src/utils/use-keyv-auth-state.js +307 -0
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +1227 -0
- 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/auto.js +139 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1727 -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 +730 -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 +22852 -0
- package/src/wam/wam.js +146 -0
- package/voip.d.ts +144 -0
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { Long } from './long.js'
|
|
2
|
+
|
|
3
|
+
export class LongBits {
|
|
4
|
+
constructor(lo, hi) {
|
|
5
|
+
this.lo = lo >>> 0
|
|
6
|
+
this.hi = hi >>> 0
|
|
7
|
+
}
|
|
8
|
+
static fromNumber(value) {
|
|
9
|
+
if (value === 0) return LongBits.zero
|
|
10
|
+
const sign = value < 0
|
|
11
|
+
if (sign) value = -value
|
|
12
|
+
let lo = value >>> 0
|
|
13
|
+
let hi = ((value - lo) / 4294967296) >>> 0
|
|
14
|
+
if (sign) {
|
|
15
|
+
hi = ~hi >>> 0
|
|
16
|
+
lo = ~lo >>> 0
|
|
17
|
+
if (++lo > 4294967295) {
|
|
18
|
+
lo = 0
|
|
19
|
+
if (++hi > 4294967295) hi = 0
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return new LongBits(lo, hi)
|
|
23
|
+
}
|
|
24
|
+
static from(value) {
|
|
25
|
+
if (typeof value === 'number') return LongBits.fromNumber(value)
|
|
26
|
+
if (typeof value === 'string' || value instanceof String) return LongBits.from(Long.fromString(String(value)))
|
|
27
|
+
return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : LongBits.zero
|
|
28
|
+
}
|
|
29
|
+
toNumber(unsigned) {
|
|
30
|
+
if (!unsigned && this.hi >>> 31) {
|
|
31
|
+
const lo = ~this.lo + 1 >>> 0
|
|
32
|
+
let hi = ~this.hi >>> 0
|
|
33
|
+
if (!lo) hi = (hi + 1) >>> 0
|
|
34
|
+
return -(lo + hi * 4294967296)
|
|
35
|
+
}
|
|
36
|
+
return this.lo + this.hi * 4294967296
|
|
37
|
+
}
|
|
38
|
+
toLong(unsigned) {
|
|
39
|
+
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
|
|
40
|
+
}
|
|
41
|
+
zzEncode() {
|
|
42
|
+
const mask = this.hi >> 31
|
|
43
|
+
this.hi = (((this.hi << 1) | (this.lo >>> 31)) ^ mask) >>> 0
|
|
44
|
+
this.lo = ((this.lo << 1) ^ mask) >>> 0
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
zzDecode() {
|
|
48
|
+
const mask = -(this.lo & 1)
|
|
49
|
+
this.lo = (((this.lo >>> 1) | (this.hi << 31)) ^ mask) >>> 0
|
|
50
|
+
this.hi = ((this.hi >>> 1) ^ mask) >>> 0
|
|
51
|
+
return this
|
|
52
|
+
}
|
|
53
|
+
length() {
|
|
54
|
+
const part0 = this.lo
|
|
55
|
+
const part1 = ((this.lo >>> 28) | (this.hi << 4)) >>> 0
|
|
56
|
+
const part2 = this.hi >>> 24
|
|
57
|
+
return part2 === 0
|
|
58
|
+
? part1 === 0
|
|
59
|
+
? part0 < 16384
|
|
60
|
+
? part0 < 128
|
|
61
|
+
? 1
|
|
62
|
+
: 2
|
|
63
|
+
: part0 < 2097152
|
|
64
|
+
? 3
|
|
65
|
+
: 4
|
|
66
|
+
: part1 < 16384
|
|
67
|
+
? part1 < 128
|
|
68
|
+
? 5
|
|
69
|
+
: 6
|
|
70
|
+
: part1 < 2097152
|
|
71
|
+
? 7
|
|
72
|
+
: 8
|
|
73
|
+
: part2 < 128
|
|
74
|
+
? 9
|
|
75
|
+
: 10
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
LongBits.zero = new LongBits(0, 0)
|
|
79
|
+
|
|
80
|
+
LongBits.zero.toNumber = () => 0
|
|
81
|
+
LongBits.zero.zzEncode = LongBits.zero.zzDecode = function () {
|
|
82
|
+
return this
|
|
83
|
+
}
|
|
84
|
+
LongBits.zero.length = () => 1
|
|
85
|
+
|
|
86
|
+
const indexOutOfRange = (reader, writeLength) =>
|
|
87
|
+
RangeError('index out of range: ' + reader.pos + ' + ' + (writeLength || 1) + ' > ' + reader.len)
|
|
88
|
+
|
|
89
|
+
function readVarint32NearEnd(reader) {
|
|
90
|
+
|
|
91
|
+
let value = 0
|
|
92
|
+
for (let i = 0; i < 4; ++i) {
|
|
93
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
94
|
+
const b = reader.buf[reader.pos++]
|
|
95
|
+
value = (value | ((b & 127) << (i * 7))) >>> 0
|
|
96
|
+
if (b < 128) return value
|
|
97
|
+
}
|
|
98
|
+
throw indexOutOfRange(reader)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readLongVarint(reader) {
|
|
102
|
+
const bits = new LongBits(0, 0)
|
|
103
|
+
let i = 0
|
|
104
|
+
if (reader.len - reader.pos > 4) {
|
|
105
|
+
|
|
106
|
+
for (; i < 4; ++i) {
|
|
107
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
108
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << 28)) >>> 0
|
|
112
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) >> 4)) >>> 0
|
|
113
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
114
|
+
i = 0
|
|
115
|
+
} else {
|
|
116
|
+
for (; i < 3; ++i) {
|
|
117
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
118
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
119
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos++] & 127) << (i * 7))) >>> 0
|
|
123
|
+
return bits
|
|
124
|
+
}
|
|
125
|
+
if (reader.len - reader.pos > 4) {
|
|
126
|
+
|
|
127
|
+
for (; i < 5; ++i) {
|
|
128
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) << (i * 7 + 3))) >>> 0
|
|
129
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
for (; i < 5; ++i) {
|
|
133
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
134
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) << (i * 7 + 3))) >>> 0
|
|
135
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
throw Error('invalid varint encoding')
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const readFixed32End = (buf, end) => (buf[end - 4] | (buf[end - 3] << 8) | (buf[end - 2] << 16) | (buf[end - 1] << 24)) >>> 0
|
|
142
|
+
|
|
143
|
+
export class Reader {
|
|
144
|
+
constructor(buffer) {
|
|
145
|
+
this.buf = buffer
|
|
146
|
+
this.pos = 0
|
|
147
|
+
this.len = buffer.length
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
static create(buffer) {
|
|
151
|
+
if (Buffer.isBuffer(buffer) || buffer instanceof Uint8Array || Array.isArray(buffer)) return new Reader(buffer)
|
|
152
|
+
throw Error('illegal buffer')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
uint32() {
|
|
156
|
+
if (this.len - this.pos < 5) {
|
|
157
|
+
if (this.pos >= this.len) throw indexOutOfRange(this)
|
|
158
|
+
if (this.buf[this.pos] >= 128) return readVarint32NearEnd(this)
|
|
159
|
+
}
|
|
160
|
+
let value = (this.buf[this.pos] & 127) >>> 0
|
|
161
|
+
if (this.buf[this.pos++] < 128) return value
|
|
162
|
+
value = (value | ((this.buf[this.pos] & 127) << 7)) >>> 0
|
|
163
|
+
if (this.buf[this.pos++] < 128) return value
|
|
164
|
+
value = (value | ((this.buf[this.pos] & 127) << 14)) >>> 0
|
|
165
|
+
if (this.buf[this.pos++] < 128) return value
|
|
166
|
+
value = (value | ((this.buf[this.pos] & 127) << 21)) >>> 0
|
|
167
|
+
if (this.buf[this.pos++] < 128) return value
|
|
168
|
+
value = (value | ((this.buf[this.pos] & 15) << 28)) >>> 0
|
|
169
|
+
if (this.buf[this.pos++] < 128) return value
|
|
170
|
+
if ((this.pos += 5) > this.len) {
|
|
171
|
+
this.pos = this.len
|
|
172
|
+
throw indexOutOfRange(this, 10)
|
|
173
|
+
}
|
|
174
|
+
return value
|
|
175
|
+
}
|
|
176
|
+
int32() {
|
|
177
|
+
return this.uint32() | 0
|
|
178
|
+
}
|
|
179
|
+
sint32() {
|
|
180
|
+
const value = this.uint32()
|
|
181
|
+
return ((value >>> 1) ^ -(value & 1)) | 0
|
|
182
|
+
}
|
|
183
|
+
int64() {
|
|
184
|
+
return readLongVarint(this).toLong(false)
|
|
185
|
+
}
|
|
186
|
+
uint64() {
|
|
187
|
+
return readLongVarint(this).toLong(true)
|
|
188
|
+
}
|
|
189
|
+
sint64() {
|
|
190
|
+
return readLongVarint(this).zzDecode().toLong(false)
|
|
191
|
+
}
|
|
192
|
+
bool() {
|
|
193
|
+
return this.uint32() !== 0
|
|
194
|
+
}
|
|
195
|
+
fixed32() {
|
|
196
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
197
|
+
return readFixed32End(this.buf, (this.pos += 4))
|
|
198
|
+
}
|
|
199
|
+
sfixed32() {
|
|
200
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
201
|
+
return readFixed32End(this.buf, (this.pos += 4)) | 0
|
|
202
|
+
}
|
|
203
|
+
_fixed64() {
|
|
204
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 8)
|
|
205
|
+
return new LongBits(readFixed32End(this.buf, (this.pos += 4)), readFixed32End(this.buf, (this.pos += 4)))
|
|
206
|
+
}
|
|
207
|
+
fixed64() {
|
|
208
|
+
return this._fixed64().toLong(true)
|
|
209
|
+
}
|
|
210
|
+
sfixed64() {
|
|
211
|
+
return this._fixed64().toLong(false)
|
|
212
|
+
}
|
|
213
|
+
float() {
|
|
214
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
215
|
+
const value = Buffer.prototype.readFloatLE.call(this.buf, this.pos)
|
|
216
|
+
this.pos += 4
|
|
217
|
+
return value
|
|
218
|
+
}
|
|
219
|
+
double() {
|
|
220
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 4)
|
|
221
|
+
const value = Buffer.prototype.readDoubleLE.call(this.buf, this.pos)
|
|
222
|
+
this.pos += 8
|
|
223
|
+
return value
|
|
224
|
+
}
|
|
225
|
+
bytes() {
|
|
226
|
+
const length = this.uint32()
|
|
227
|
+
const start = this.pos
|
|
228
|
+
const end = this.pos + length
|
|
229
|
+
if (end > this.len) throw indexOutOfRange(this, length)
|
|
230
|
+
this.pos += length
|
|
231
|
+
if (Array.isArray(this.buf)) return this.buf.slice(start, end)
|
|
232
|
+
if (start === end) return Buffer.alloc(0)
|
|
233
|
+
|
|
234
|
+
return Buffer.isBuffer(this.buf) ? this.buf.slice(start, end) : this.buf.subarray(start, end)
|
|
235
|
+
}
|
|
236
|
+
string() {
|
|
237
|
+
if (Buffer.isBuffer(this.buf)) {
|
|
238
|
+
const len = this.uint32()
|
|
239
|
+
|
|
240
|
+
return this.buf.utf8Slice(this.pos, (this.pos = Math.min(this.pos + len, this.len)))
|
|
241
|
+
}
|
|
242
|
+
const b = this.bytes()
|
|
243
|
+
return Buffer.from(b.buffer, b.byteOffset, b.byteLength).toString('utf8')
|
|
244
|
+
}
|
|
245
|
+
skip(length) {
|
|
246
|
+
if (typeof length === 'number') {
|
|
247
|
+
if (this.pos + length > this.len) throw indexOutOfRange(this, length)
|
|
248
|
+
this.pos += length
|
|
249
|
+
} else {
|
|
250
|
+
do {
|
|
251
|
+
if (this.pos >= this.len) throw indexOutOfRange(this)
|
|
252
|
+
} while (this.buf[this.pos++] & 128)
|
|
253
|
+
}
|
|
254
|
+
return this
|
|
255
|
+
}
|
|
256
|
+
skipType(wireType, depth) {
|
|
257
|
+
if (depth === undefined) depth = 0
|
|
258
|
+
if (depth > Reader.recursionLimit) throw Error('maximum nesting depth exceeded')
|
|
259
|
+
switch (wireType) {
|
|
260
|
+
case 0:
|
|
261
|
+
this.skip()
|
|
262
|
+
break
|
|
263
|
+
case 1:
|
|
264
|
+
this.skip(8)
|
|
265
|
+
break
|
|
266
|
+
case 2:
|
|
267
|
+
this.skip(this.uint32())
|
|
268
|
+
break
|
|
269
|
+
case 3:
|
|
270
|
+
while ((wireType = this.uint32() & 7) !== 4) this.skipType(wireType, depth + 1)
|
|
271
|
+
break
|
|
272
|
+
case 5:
|
|
273
|
+
this.skip(4)
|
|
274
|
+
break
|
|
275
|
+
default:
|
|
276
|
+
throw Error('invalid wire type ' + wireType + ' at offset ' + this.pos)
|
|
277
|
+
}
|
|
278
|
+
return this
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
Reader.recursionLimit = 100
|
|
282
|
+
|
|
283
|
+
class Op {
|
|
284
|
+
constructor(fn, len, val) {
|
|
285
|
+
this.fn = fn
|
|
286
|
+
this.len = len
|
|
287
|
+
this.next = undefined
|
|
288
|
+
this.val = val
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const noop = () => {}
|
|
292
|
+
|
|
293
|
+
class State {
|
|
294
|
+
constructor(writer) {
|
|
295
|
+
this.head = writer.head
|
|
296
|
+
this.tail = writer.tail
|
|
297
|
+
this.len = writer.len
|
|
298
|
+
this.next = writer.states
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const writeByte = (val, buf, pos) => {
|
|
303
|
+
buf[pos] = val & 255
|
|
304
|
+
}
|
|
305
|
+
const writeVarint32 = (val, buf, pos) => {
|
|
306
|
+
while (val > 127) {
|
|
307
|
+
buf[pos++] = (val & 127) | 128
|
|
308
|
+
val >>>= 7
|
|
309
|
+
}
|
|
310
|
+
buf[pos] = val
|
|
311
|
+
}
|
|
312
|
+
const writeVarint64 = (val, buf, pos) => {
|
|
313
|
+
let lo = val.lo
|
|
314
|
+
let hi = val.hi
|
|
315
|
+
while (hi) {
|
|
316
|
+
buf[pos++] = (lo & 127) | 128
|
|
317
|
+
lo = ((lo >>> 7) | (hi << 25)) >>> 0
|
|
318
|
+
hi >>>= 7
|
|
319
|
+
}
|
|
320
|
+
while (lo > 127) {
|
|
321
|
+
buf[pos++] = (lo & 127) | 128
|
|
322
|
+
lo = lo >>> 7
|
|
323
|
+
}
|
|
324
|
+
buf[pos++] = lo
|
|
325
|
+
}
|
|
326
|
+
const writeFixed32 = (val, buf, pos) => {
|
|
327
|
+
buf[pos] = val & 255
|
|
328
|
+
buf[pos + 1] = (val >>> 8) & 255
|
|
329
|
+
buf[pos + 2] = (val >>> 16) & 255
|
|
330
|
+
buf[pos + 3] = val >>> 24
|
|
331
|
+
}
|
|
332
|
+
const writeFloat = (val, buf, pos) => {
|
|
333
|
+
Buffer.prototype.writeFloatLE.call(buf, val, pos)
|
|
334
|
+
}
|
|
335
|
+
const writeDouble = (val, buf, pos) => {
|
|
336
|
+
Buffer.prototype.writeDoubleLE.call(buf, val, pos)
|
|
337
|
+
}
|
|
338
|
+
const writeBytes = (val, buf, pos) => {
|
|
339
|
+
buf.set(val, pos)
|
|
340
|
+
}
|
|
341
|
+
const writeString = (val, buf, pos) => {
|
|
342
|
+
buf.utf8Write(val, pos)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export class Writer {
|
|
346
|
+
constructor() {
|
|
347
|
+
this.len = 0
|
|
348
|
+
this.head = new Op(noop, 0, 0)
|
|
349
|
+
this.tail = this.head
|
|
350
|
+
this.states = null
|
|
351
|
+
}
|
|
352
|
+
static create() {
|
|
353
|
+
return new Writer()
|
|
354
|
+
}
|
|
355
|
+
static alloc(size) {
|
|
356
|
+
return Buffer.allocUnsafe(size)
|
|
357
|
+
}
|
|
358
|
+
_push(fn, len, val) {
|
|
359
|
+
this.tail = this.tail.next = new Op(fn, len, val)
|
|
360
|
+
this.len += len
|
|
361
|
+
return this
|
|
362
|
+
}
|
|
363
|
+
uint32(value) {
|
|
364
|
+
value = value >>> 0
|
|
365
|
+
return this._push(writeVarint32, value < 128 ? 1 : value < 16384 ? 2 : value < 2097152 ? 3 : value < 268435456 ? 4 : 5, value)
|
|
366
|
+
}
|
|
367
|
+
int32(value) {
|
|
368
|
+
return (value |= 0) < 0
|
|
369
|
+
? this._push(writeVarint64, 10, LongBits.fromNumber(value))
|
|
370
|
+
: this.uint32(value)
|
|
371
|
+
}
|
|
372
|
+
sint32(value) {
|
|
373
|
+
return this.uint32(((value << 1) ^ (value >> 31)) >>> 0)
|
|
374
|
+
}
|
|
375
|
+
uint64(value) {
|
|
376
|
+
const bits = LongBits.from(value)
|
|
377
|
+
return this._push(writeVarint64, bits.length(), bits)
|
|
378
|
+
}
|
|
379
|
+
int64(value) {
|
|
380
|
+
return this.uint64(value)
|
|
381
|
+
}
|
|
382
|
+
sint64(value) {
|
|
383
|
+
const bits = LongBits.from(value).zzEncode()
|
|
384
|
+
return this._push(writeVarint64, bits.length(), bits)
|
|
385
|
+
}
|
|
386
|
+
bool(value) {
|
|
387
|
+
return this._push(writeByte, 1, value ? 1 : 0)
|
|
388
|
+
}
|
|
389
|
+
fixed32(value) {
|
|
390
|
+
return this._push(writeFixed32, 4, value >>> 0)
|
|
391
|
+
}
|
|
392
|
+
sfixed32(value) {
|
|
393
|
+
return this.fixed32(value)
|
|
394
|
+
}
|
|
395
|
+
fixed64(value) {
|
|
396
|
+
const bits = LongBits.from(value)
|
|
397
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi)
|
|
398
|
+
}
|
|
399
|
+
sfixed64(value) {
|
|
400
|
+
return this.fixed64(value)
|
|
401
|
+
}
|
|
402
|
+
float(value) {
|
|
403
|
+
return this._push(writeFloat, 4, value)
|
|
404
|
+
}
|
|
405
|
+
double(value) {
|
|
406
|
+
return this._push(writeDouble, 8, value)
|
|
407
|
+
}
|
|
408
|
+
bytes(value) {
|
|
409
|
+
|
|
410
|
+
if (typeof value === 'string' || value instanceof String) value = Buffer.from(value, 'base64')
|
|
411
|
+
const len = value.length >>> 0
|
|
412
|
+
this.uint32(len)
|
|
413
|
+
if (len) this._push(writeBytes, len, value)
|
|
414
|
+
return this
|
|
415
|
+
}
|
|
416
|
+
string(value) {
|
|
417
|
+
const len = Buffer.byteLength(value)
|
|
418
|
+
this.uint32(len)
|
|
419
|
+
if (len) this._push(writeString, len, value)
|
|
420
|
+
return this
|
|
421
|
+
}
|
|
422
|
+
fork() {
|
|
423
|
+
this.states = new State(this)
|
|
424
|
+
this.head = this.tail = new Op(noop, 0, 0)
|
|
425
|
+
this.len = 0
|
|
426
|
+
return this
|
|
427
|
+
}
|
|
428
|
+
reset() {
|
|
429
|
+
if (this.states) {
|
|
430
|
+
this.head = this.states.head
|
|
431
|
+
this.tail = this.states.tail
|
|
432
|
+
this.len = this.states.len
|
|
433
|
+
this.states = this.states.next
|
|
434
|
+
} else {
|
|
435
|
+
this.head = this.tail = new Op(noop, 0, 0)
|
|
436
|
+
this.len = 0
|
|
437
|
+
}
|
|
438
|
+
return this
|
|
439
|
+
}
|
|
440
|
+
ldelim() {
|
|
441
|
+
const head = this.head
|
|
442
|
+
const tail = this.tail
|
|
443
|
+
const len = this.len
|
|
444
|
+
this.reset().uint32(len)
|
|
445
|
+
if (len) {
|
|
446
|
+
this.tail.next = head.next
|
|
447
|
+
this.tail = tail
|
|
448
|
+
this.len += len
|
|
449
|
+
}
|
|
450
|
+
return this
|
|
451
|
+
}
|
|
452
|
+
finish() {
|
|
453
|
+
let head = this.head.next
|
|
454
|
+
const buf = Writer.alloc(this.len)
|
|
455
|
+
let pos = 0
|
|
456
|
+
while (head) {
|
|
457
|
+
head.fn(head.val, buf, pos)
|
|
458
|
+
pos += head.len
|
|
459
|
+
head = head.next
|
|
460
|
+
}
|
|
461
|
+
return buf
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const B64 = new Array(64)
|
|
466
|
+
const S64 = new Array(123)
|
|
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++
|
|
468
|
+
|
|
469
|
+
const isUnsafeProperty = key => key === '__proto__' || key === 'prototype' || key === 'constructor'
|
|
470
|
+
|
|
471
|
+
export const util = {
|
|
472
|
+
Long,
|
|
473
|
+
LongBits,
|
|
474
|
+
isUnsafeProperty,
|
|
475
|
+
emptyArray: Object.freeze([]),
|
|
476
|
+
emptyObject: Object.freeze({}),
|
|
477
|
+
recursionLimit: 100,
|
|
478
|
+
nestingLimit: 32,
|
|
479
|
+
toJSONOptions: { longs: String, enums: String, bytes: String, json: true },
|
|
480
|
+
newBuffer: sizeOrArray => (typeof sizeOrArray === 'number' ? Buffer.allocUnsafe(sizeOrArray) : Buffer.from(sizeOrArray)),
|
|
481
|
+
base64: {
|
|
482
|
+
|
|
483
|
+
length(string) {
|
|
484
|
+
let p = string.length
|
|
485
|
+
if (!p) return 0
|
|
486
|
+
let n = 0
|
|
487
|
+
while (--p % 4 > 1 && string.charAt(p) === '=') ++n
|
|
488
|
+
return Math.ceil(string.length * 3) / 4 - n
|
|
489
|
+
},
|
|
490
|
+
|
|
491
|
+
encode(buffer, start, end) {
|
|
492
|
+
let parts = null
|
|
493
|
+
let chunk = []
|
|
494
|
+
let i = 0
|
|
495
|
+
let j = 0
|
|
496
|
+
let t
|
|
497
|
+
while (start < end) {
|
|
498
|
+
const b = buffer[start++]
|
|
499
|
+
switch (j) {
|
|
500
|
+
case 0:
|
|
501
|
+
chunk[i++] = B64[b >> 2]
|
|
502
|
+
t = (b & 3) << 4
|
|
503
|
+
j = 1
|
|
504
|
+
break
|
|
505
|
+
case 1:
|
|
506
|
+
chunk[i++] = B64[t | (b >> 4)]
|
|
507
|
+
t = (b & 15) << 2
|
|
508
|
+
j = 2
|
|
509
|
+
break
|
|
510
|
+
case 2:
|
|
511
|
+
chunk[i++] = B64[t | (b >> 6)]
|
|
512
|
+
chunk[i++] = B64[b & 63]
|
|
513
|
+
j = 0
|
|
514
|
+
break
|
|
515
|
+
}
|
|
516
|
+
if (i > 8191) {
|
|
517
|
+
;(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk))
|
|
518
|
+
i = 0
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (j) {
|
|
522
|
+
chunk[i++] = B64[t]
|
|
523
|
+
chunk[i++] = 61
|
|
524
|
+
if (j === 1) chunk[i++] = 61
|
|
525
|
+
}
|
|
526
|
+
if (parts) {
|
|
527
|
+
if (i) parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)))
|
|
528
|
+
return parts.join('')
|
|
529
|
+
}
|
|
530
|
+
return String.fromCharCode.apply(String, chunk.slice(0, i))
|
|
531
|
+
},
|
|
532
|
+
|
|
533
|
+
decode(string, buffer, offset) {
|
|
534
|
+
const start = offset
|
|
535
|
+
let j = 0
|
|
536
|
+
let t
|
|
537
|
+
for (let i = 0; i < string.length; ) {
|
|
538
|
+
let c = string.charCodeAt(i++)
|
|
539
|
+
if (c === 61 && j > 1) break
|
|
540
|
+
c = S64[c]
|
|
541
|
+
if (c === undefined) throw Error('invalid encoding')
|
|
542
|
+
switch (j) {
|
|
543
|
+
case 0:
|
|
544
|
+
t = c
|
|
545
|
+
j = 1
|
|
546
|
+
break
|
|
547
|
+
case 1:
|
|
548
|
+
buffer[offset++] = (t << 2) | ((c & 48) >> 4)
|
|
549
|
+
t = c
|
|
550
|
+
j = 2
|
|
551
|
+
break
|
|
552
|
+
case 2:
|
|
553
|
+
buffer[offset++] = ((t & 15) << 4) | ((c & 60) >> 2)
|
|
554
|
+
t = c
|
|
555
|
+
j = 3
|
|
556
|
+
break
|
|
557
|
+
case 3:
|
|
558
|
+
buffer[offset++] = ((t & 3) << 6) | c
|
|
559
|
+
j = 0
|
|
560
|
+
break
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
if (j === 1) throw Error('invalid encoding')
|
|
564
|
+
return offset - start
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
makeProp(obj, key) {
|
|
568
|
+
Object.defineProperty(obj, key, { enumerable: true, configurable: true, writable: true })
|
|
569
|
+
},
|
|
570
|
+
oneOfGetter(fieldNames) {
|
|
571
|
+
const fieldMap = {}
|
|
572
|
+
for (let i = 0; i < fieldNames.length; ++i) fieldMap[fieldNames[i]] = 1
|
|
573
|
+
return function () {
|
|
574
|
+
for (let keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
|
|
575
|
+
if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null) return keys[i]
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
oneOfSetter(fieldNames) {
|
|
579
|
+
return function (name) {
|
|
580
|
+
for (let i = 0; i < fieldNames.length; ++i) if (fieldNames[i] !== name) delete this[fieldNames[i]]
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
export const roots = {}
|
|
586
|
+
|
|
587
|
+
export default { Reader, Writer, util, roots, LongBits }
|