nodejs-insta-private-api-mqt 1.3.73 → 1.3.74
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/dist/mqttot/mqttot.client.js +3 -54
- package/dist/mqttot/mqttot.connect.request.packet.js +1 -1
- package/dist/mqttot/mqttot.connection.js +1 -3
- package/dist/realtime/topic.js +1 -1
- package/dist/thrift/thrift.js +99 -89
- package/dist/thrift/thrift.reading.js +335 -294
- package/dist/thrift/thrift.writing.js +380 -326
- package/package.json +1 -1
|
@@ -3,340 +3,394 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BufferWriter = exports.thriftWriteFromObject = void 0;
|
|
4
4
|
const thrift_1 = require("./thrift");
|
|
5
5
|
const errors_1 = require("../errors");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Thrift Compact Protocol writer (subset) optimized for MQTT-over-Thrift payloads.
|
|
9
|
+
* Improvements vs old version:
|
|
10
|
+
* - avoids Buffer.concat per write (uses chunk list + lazy concat)
|
|
11
|
+
* - faster descriptor lookup via Map
|
|
12
|
+
* - more flexible INT_64 input (number|bigint|string|{int}|Long-like)
|
|
13
|
+
*/
|
|
14
|
+
|
|
6
15
|
function thriftWriteFromObject(obj, descriptors) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
const writer = BufferWriter.empty();
|
|
17
|
+
thriftWriteSingleLayerFromObject(obj, descriptors, writer);
|
|
18
|
+
writer.writeStop();
|
|
19
|
+
return writer.buffer;
|
|
11
20
|
}
|
|
12
21
|
exports.thriftWriteFromObject = thriftWriteFromObject;
|
|
22
|
+
|
|
23
|
+
function buildDescriptorMap(descriptors) {
|
|
24
|
+
const map = new Map();
|
|
25
|
+
for (const d of descriptors) map.set(d.fieldName, d);
|
|
26
|
+
return map;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function normalizeInt64(v, nameForError) {
|
|
30
|
+
if (typeof v === "bigint") return v;
|
|
31
|
+
if (typeof v === "number") return BigInt(v);
|
|
32
|
+
if (typeof v === "string") return BigInt(v);
|
|
33
|
+
if (v && typeof v === "object") {
|
|
34
|
+
if (typeof v.int === "bigint") return v.int; // our reader shape
|
|
35
|
+
if (typeof v.toString === "function") return BigInt(v.toString());
|
|
36
|
+
}
|
|
37
|
+
throw new errors_1.IllegalArgumentError(`Value of ${nameForError} is neither a bigint nor a number/string`);
|
|
38
|
+
}
|
|
39
|
+
|
|
13
40
|
function thriftWriteSingleLayerFromObject(obj, descriptors, writer) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
writer.writeString(descriptor.field, value);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
throw new errors_1.IllegalArgumentError(`Value of ${name} is not a string`);
|
|
86
|
-
}
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
case thrift_1.ThriftTypes.MAP: {
|
|
90
|
-
if (descriptor.type === thrift_1.ThriftTypes.MAP_BINARY_BINARY) {
|
|
91
|
-
let pairs;
|
|
92
|
-
if (Array.isArray(value)) {
|
|
93
|
-
//[{key: 'a', value: 'b'}]
|
|
94
|
-
pairs = value.map((x) => [x.key, x.value]);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
// {a: 'b'}
|
|
98
|
-
pairs = Object.entries(value);
|
|
99
|
-
}
|
|
100
|
-
writer.writeMapHeader(descriptor.field, pairs.length, thrift_1.ThriftTypes.BINARY, thrift_1.ThriftTypes.BINARY);
|
|
101
|
-
if (pairs.length !== 0) {
|
|
102
|
-
for (const pair of pairs) {
|
|
103
|
-
writer.writeStringDirect(pair[0]).writeStringDirect(pair[1]);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
throw new errors_1.ThriftError(`Map of type ${descriptor.type} not impl.`);
|
|
109
|
-
}
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
default: {
|
|
113
|
-
throw new errors_1.ThriftError(`Could not find type ${descriptor.type} for ${name}`);
|
|
114
|
-
}
|
|
41
|
+
const descriptorMap = buildDescriptorMap(descriptors);
|
|
42
|
+
const entries = Object.entries(obj);
|
|
43
|
+
|
|
44
|
+
for (const [name, value] of entries) {
|
|
45
|
+
if (typeof value === "undefined") continue;
|
|
46
|
+
|
|
47
|
+
const descriptor = descriptorMap.get(name);
|
|
48
|
+
if (!descriptor) throw new errors_1.IllegalArgumentError(`Descriptor for ${name} not found`);
|
|
49
|
+
|
|
50
|
+
switch (descriptor.type & 0xff) {
|
|
51
|
+
case thrift_1.ThriftTypes.BOOLEAN:
|
|
52
|
+
case thrift_1.ThriftTypes.TRUE:
|
|
53
|
+
case thrift_1.ThriftTypes.FALSE: {
|
|
54
|
+
writer.writeBoolean(descriptor.field, !!value);
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case thrift_1.ThriftTypes.BYTE: {
|
|
58
|
+
if (typeof value === "number") writer.writeInt8(descriptor.field, value);
|
|
59
|
+
else throw new errors_1.IllegalArgumentError(`Value of ${name} is not a number`);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case thrift_1.ThriftTypes.INT_16: {
|
|
63
|
+
if (typeof value === "number") writer.writeInt16(descriptor.field, value);
|
|
64
|
+
else throw new errors_1.IllegalArgumentError(`Value of ${name} is not a number`);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case thrift_1.ThriftTypes.INT_32: {
|
|
68
|
+
if (typeof value === "number") writer.writeInt32(descriptor.field, value);
|
|
69
|
+
else throw new errors_1.IllegalArgumentError(`Value of ${name} is not a number`);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case thrift_1.ThriftTypes.INT_64: {
|
|
73
|
+
writer.writeInt64Buffer(descriptor.field, normalizeInt64(value, name));
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case thrift_1.ThriftTypes.LIST: {
|
|
77
|
+
// packed list type is stored in high byte (descriptor.type >> 8)
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
writer.writeList(descriptor.field, descriptor.type >> 8, value);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case thrift_1.ThriftTypes.STRUCT: {
|
|
83
|
+
writer.writeStruct(descriptor.field);
|
|
84
|
+
thriftWriteSingleLayerFromObject(value, descriptor.structDescriptors ?? [], writer);
|
|
85
|
+
writer.writeStop();
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case thrift_1.ThriftTypes.BINARY: {
|
|
89
|
+
if (typeof value === "string") writer.writeString(descriptor.field, value);
|
|
90
|
+
else throw new errors_1.IllegalArgumentError(`Value of ${name} is not a string`);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case thrift_1.ThriftTypes.MAP: {
|
|
94
|
+
if (descriptor.type === thrift_1.ThriftTypes.MAP_BINARY_BINARY) {
|
|
95
|
+
let pairs;
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
// [{key:'a', value:'b'}]
|
|
98
|
+
pairs = value.map((x) => [x.key, x.value]);
|
|
99
|
+
} else {
|
|
100
|
+
// {a:'b'}
|
|
101
|
+
pairs = Object.entries(value);
|
|
102
|
+
}
|
|
103
|
+
writer.writeMapHeader(descriptor.field, pairs.length, thrift_1.ThriftTypes.BINARY, thrift_1.ThriftTypes.BINARY);
|
|
104
|
+
if (pairs.length !== 0) {
|
|
105
|
+
for (const [k, v] of pairs) writer.writeStringDirect(k).writeStringDirect(v);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
throw new errors_1.ThriftError(`Map of type ${descriptor.type} not impl.`);
|
|
115
109
|
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
default: {
|
|
113
|
+
throw new errors_1.ThriftError(`Could not find type ${descriptor.type} for ${name}`);
|
|
114
|
+
}
|
|
116
115
|
}
|
|
116
|
+
}
|
|
117
117
|
}
|
|
118
|
+
|
|
118
119
|
class BufferWriter {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
120
|
+
get buffer() {
|
|
121
|
+
// Lazy concat: only when requested
|
|
122
|
+
if (this._dirty) {
|
|
123
|
+
this._buffer = Buffer.concat(this._chunks, this._length);
|
|
124
|
+
this._dirty = false;
|
|
125
|
+
// free chunks to reduce memory, keep single buffer from now on
|
|
126
|
+
this._chunks = [this._buffer];
|
|
127
|
+
}
|
|
128
|
+
return this._buffer;
|
|
129
|
+
}
|
|
130
|
+
get position() {
|
|
131
|
+
return this._position;
|
|
132
|
+
}
|
|
133
|
+
get length() {
|
|
134
|
+
return this._length;
|
|
135
|
+
}
|
|
136
|
+
get field() {
|
|
137
|
+
return this._field;
|
|
138
|
+
}
|
|
139
|
+
get stack() {
|
|
140
|
+
return this._stack;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
constructor(data, length, buffer) {
|
|
144
|
+
this._position = 0;
|
|
145
|
+
this._field = 0;
|
|
146
|
+
this._stack = [];
|
|
147
|
+
|
|
148
|
+
// internal chunked buffer state
|
|
149
|
+
this._chunks = [];
|
|
150
|
+
this._length = 0;
|
|
151
|
+
this._dirty = true;
|
|
152
|
+
|
|
153
|
+
if (data) {
|
|
154
|
+
const b = Buffer.from(data);
|
|
155
|
+
this._chunks.push(b);
|
|
156
|
+
this._length = b.length;
|
|
157
|
+
this._buffer = b;
|
|
158
|
+
this._dirty = false;
|
|
159
|
+
} else if (typeof length === "number") {
|
|
160
|
+
// not used in current library, but keep for compatibility
|
|
161
|
+
const b = Buffer.alloc(length);
|
|
162
|
+
this._chunks.push(b);
|
|
163
|
+
this._length = b.length;
|
|
164
|
+
this._buffer = b;
|
|
165
|
+
this._dirty = false;
|
|
166
|
+
} else if (buffer) {
|
|
167
|
+
this._chunks.push(buffer);
|
|
168
|
+
this._length = buffer.length;
|
|
169
|
+
this._buffer = buffer;
|
|
170
|
+
this._dirty = false;
|
|
171
|
+
} else {
|
|
172
|
+
const b = Buffer.from([]);
|
|
173
|
+
this._chunks.push(b);
|
|
174
|
+
this._length = 0;
|
|
175
|
+
this._buffer = b;
|
|
176
|
+
this._dirty = false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static fromLength(len) {
|
|
181
|
+
return new BufferWriter(undefined, len, undefined);
|
|
182
|
+
}
|
|
183
|
+
static fromBuffer(buf) {
|
|
184
|
+
return new BufferWriter(undefined, undefined, buf);
|
|
185
|
+
}
|
|
186
|
+
static fromString(data) {
|
|
187
|
+
return new BufferWriter(data, undefined, undefined);
|
|
188
|
+
}
|
|
189
|
+
static empty() {
|
|
190
|
+
return new BufferWriter(undefined, undefined, undefined);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
move(bytes) {
|
|
194
|
+
this._position = this._position + bytes;
|
|
195
|
+
return this._position - bytes;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
writeVarInt(num) {
|
|
199
|
+
// num is int32-ish here
|
|
200
|
+
while (true) {
|
|
201
|
+
let byte = num & ~0x7f;
|
|
202
|
+
if (byte === 0) {
|
|
203
|
+
this.writeByte(num);
|
|
204
|
+
break;
|
|
205
|
+
} else if (byte === -128) {
|
|
206
|
+
// -128 = 0b1000_0000 but it's the last and no other bytes will follow
|
|
207
|
+
this.writeByte(0);
|
|
208
|
+
break;
|
|
209
|
+
} else {
|
|
210
|
+
byte = (num & 0xff) | 0x80;
|
|
211
|
+
this.writeByte(byte);
|
|
212
|
+
num = num >> 7;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
writeField(field, type) {
|
|
219
|
+
const delta = field - this.field;
|
|
220
|
+
if (delta > 0 && delta <= 15) {
|
|
221
|
+
this.writeByte((delta << 4) | type);
|
|
222
|
+
} else {
|
|
223
|
+
this.writeByte(type);
|
|
224
|
+
this.writeWord(field);
|
|
225
|
+
}
|
|
226
|
+
this._field = field;
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
writeBuffer(buf) {
|
|
231
|
+
// Chunk append (no concat)
|
|
232
|
+
if (!buf || buf.length === 0) return this;
|
|
233
|
+
this._chunks.push(buf);
|
|
234
|
+
this._length += buf.length;
|
|
235
|
+
this._dirty = true;
|
|
236
|
+
this.move(buf.length);
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
writeByte(byte) {
|
|
241
|
+
const b = Buffer.allocUnsafe(1);
|
|
242
|
+
b[0] = byte & 0xff;
|
|
243
|
+
return this.writeBuffer(b);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
writeWord(num) {
|
|
247
|
+
return this.writeVarInt(BufferWriter.toZigZag(num, 0x10));
|
|
248
|
+
}
|
|
249
|
+
writeInt(num) {
|
|
250
|
+
return this.writeVarInt(BufferWriter.toZigZag(num, 0x20));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
writeLong(num) {
|
|
254
|
+
if (typeof num === "object") num = num.int;
|
|
255
|
+
if (typeof num !== "bigint") num = BigInt(num);
|
|
256
|
+
this.writeBigint(BufferWriter.bigintToZigZag(num));
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
writeBigint(n) {
|
|
261
|
+
while (true) {
|
|
262
|
+
if ((n & ~BigInt(0x7f)) === BigInt(0)) {
|
|
263
|
+
this.writeByte(Number(n));
|
|
264
|
+
break;
|
|
265
|
+
} else {
|
|
266
|
+
this.writeByte(Number((n & BigInt(0x7f)) | BigInt(0x80)));
|
|
267
|
+
n = n >> BigInt(7);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
writeMapHeader(field, size, keyType, valueType) {
|
|
273
|
+
this.writeField(field, thrift_1.ThriftTypes.MAP);
|
|
274
|
+
if (size === 0) {
|
|
275
|
+
this.writeByte(0);
|
|
276
|
+
} else {
|
|
277
|
+
this.writeVarInt(size);
|
|
278
|
+
this.writeByte(((keyType & 0xf) << 4) | (valueType & 0xf));
|
|
279
|
+
}
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
writeBoolean(field, bool) {
|
|
284
|
+
return this.writeField(field, bool ? thrift_1.ThriftTypes.TRUE : thrift_1.ThriftTypes.FALSE);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
writeString(field, s) {
|
|
288
|
+
this.writeField(field, thrift_1.ThriftTypes.BINARY);
|
|
289
|
+
return this.writeStringDirect(s);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
writeStringDirect(s) {
|
|
293
|
+
const buf = Buffer.from(s, "utf8");
|
|
294
|
+
this.writeVarInt(buf.length);
|
|
295
|
+
this.writeBuffer(buf);
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
writeStop() {
|
|
300
|
+
this.writeByte(thrift_1.ThriftTypes.STOP);
|
|
301
|
+
if (this.stack.length > 0) this.popStack();
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
writeInt8(field, num) {
|
|
306
|
+
this.writeField(field, thrift_1.ThriftTypes.BYTE);
|
|
307
|
+
return this.writeByte(num);
|
|
308
|
+
}
|
|
309
|
+
writeInt16(field, num) {
|
|
310
|
+
this.writeField(field, thrift_1.ThriftTypes.INT_16);
|
|
311
|
+
return this.writeWord(num);
|
|
312
|
+
}
|
|
313
|
+
writeInt32(field, num) {
|
|
314
|
+
this.writeField(field, thrift_1.ThriftTypes.INT_32);
|
|
315
|
+
return this.writeInt(num);
|
|
316
|
+
}
|
|
317
|
+
writeInt64Buffer(field, num) {
|
|
318
|
+
this.writeField(field, thrift_1.ThriftTypes.INT_64);
|
|
319
|
+
return this.writeLong(num);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
writeList(field, type, list) {
|
|
323
|
+
this.writeField(field, thrift_1.ThriftTypes.LIST);
|
|
324
|
+
const size = list.length;
|
|
325
|
+
|
|
326
|
+
if (size < 0x0f) {
|
|
327
|
+
this.writeByte((size << 4) | type);
|
|
328
|
+
} else {
|
|
329
|
+
this.writeByte(0xf0 | type);
|
|
330
|
+
this.writeVarInt(size);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
switch (type) {
|
|
334
|
+
case thrift_1.ThriftTypes.TRUE:
|
|
335
|
+
case thrift_1.ThriftTypes.FALSE: {
|
|
336
|
+
list.forEach((el) => this.writeByte(el ? thrift_1.ThriftTypes.TRUE : thrift_1.ThriftTypes.FALSE));
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case thrift_1.ThriftTypes.BYTE: {
|
|
340
|
+
list.forEach((el) => this.writeByte(el));
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case thrift_1.ThriftTypes.INT_16: {
|
|
344
|
+
list.forEach((el) => this.writeWord(el));
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
case thrift_1.ThriftTypes.INT_32: {
|
|
348
|
+
list.forEach((el) => this.writeInt(el));
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
case thrift_1.ThriftTypes.INT_64: {
|
|
352
|
+
list.forEach((el) => this.writeLong(normalizeInt64(el, "list<int64> item")));
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case thrift_1.ThriftTypes.BINARY: {
|
|
356
|
+
list.forEach((el) => {
|
|
357
|
+
const buf = Buffer.from(el, "utf8");
|
|
358
|
+
this.writeVarInt(buf.length);
|
|
359
|
+
this.writeBuffer(buf);
|
|
360
|
+
});
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
default: {
|
|
364
|
+
throw new errors_1.ThriftError("not impl");
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
writeStruct(field) {
|
|
372
|
+
this.writeField(field, thrift_1.ThriftTypes.STRUCT);
|
|
373
|
+
this.pushStack();
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
pushStack() {
|
|
378
|
+
this._stack.push(this.field);
|
|
379
|
+
this._field = 0;
|
|
380
|
+
}
|
|
381
|
+
popStack() {
|
|
382
|
+
this._field = this._stack.pop() ?? -1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
toString() {
|
|
386
|
+
return this.buffer.toString("ascii");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
static bigintToZigZag(n) {
|
|
390
|
+
return (n << BigInt(1)) ^ (n >> BigInt(63));
|
|
391
|
+
}
|
|
339
392
|
}
|
|
340
393
|
exports.BufferWriter = BufferWriter;
|
|
394
|
+
|
|
341
395
|
BufferWriter.toZigZag = (n, bits) => (n << 1) ^ (n >> (bits - 1));
|
|
342
|
-
|
|
396
|
+
|