-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathwriter.js
743 lines (671 loc) · 20.6 KB
/
writer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/**
* @fileoverview Implements Writer for writing data as the binary wire format
* bytes array.
*/
goog.module('protobuf.binary.Writer');
const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const WireType = goog.require('protobuf.binary.WireType');
const {POLYFILL_TEXT_ENCODING, checkFieldNumber, checkTypeUnsignedInt32, checkWireType} = goog.require('protobuf.internal.checks');
const {concatenateByteArrays} = goog.require('protobuf.binary.uint8arrays');
const {createTag, getTagLength} = goog.require('protobuf.binary.tag');
const {encode} = goog.require('protobuf.binary.textencoding');
/**
* Returns a valid utf-8 encoder function based on TextEncoder if available or
* a polyfill.
* Some of the environments we run in do not have TextEncoder defined.
* TextEncoder is faster than our polyfill so we prefer it over the polyfill.
* @return {function(string):!Uint8Array}
*/
function getEncoderFunction() {
if (goog.global['TextEncoder']) {
const textEncoder = new goog.global['TextEncoder']('utf-8');
return s => s.length === 0 ? new Uint8Array(0) : textEncoder.encode(s);
}
if (POLYFILL_TEXT_ENCODING) {
return encode;
} else {
throw new Error(
'TextEncoder is missing. ' +
'Enable protobuf.defines.POLYFILL_TEXT_ENCODING');
}
}
/** @const {function(string): !Uint8Array} */
const encoderFunction = getEncoderFunction();
/**
* Writer provides methods for encoding all protobuf supported type into a
* binary format bytes array.
* Check https://protobuf.dev/programming-guides/encoding/ for binary
* format definition.
* @final
* @package
*/
class Writer {
constructor() {
/**
* Blocks of data that needs to be serialized. After writing all the data,
* the blocks are concatenated into a single Uint8Array.
* @private {!Array<!Uint8Array>}
*/
this.blocks_ = [];
/**
* A buffer for writing varint data (tag number + field number for each
* field, int32, uint32 etc.). Before writing a non-varint data block
* (string, fixed32 etc.), the buffer is appended to the block array as a
* new block, and a new buffer is started.
*
* We could've written each varint as a new block instead of writing
* multiple varints in this buffer. But this will increase the number of
* blocks, and concatenating many small blocks is slower than concatenating
* few large blocks.
*
* TODO: Experiment with writing data in a fixed-length
* Uint8Array instead of using a growing buffer.
*
* @private {!Array<number>}
*/
this.currentBuffer_ = [];
}
/**
* Converts the encoded data into a Uint8Array.
* The writer is also reset.
* @return {!ArrayBuffer}
*/
getAndResetResultBuffer() {
this.closeAndStartNewBuffer_();
const result = concatenateByteArrays(this.blocks_);
this.blocks_ = [];
return result.buffer;
}
/**
* Encodes a (field number, wire type) tuple into a wire-format field header.
* @param {number} fieldNumber
* @param {!WireType} wireType
*/
writeTag(fieldNumber, wireType) {
checkFieldNumber(fieldNumber);
checkWireType(wireType);
const tag = createTag(wireType, fieldNumber);
this.writeUnsignedVarint32_(tag);
}
/**
* Appends the current buffer into the blocks array and starts a new buffer.
* @private
*/
closeAndStartNewBuffer_() {
this.blocks_.push(new Uint8Array(this.currentBuffer_));
this.currentBuffer_ = [];
}
/**
* Encodes a 32-bit integer into its wire-format varint representation and
* stores it in the buffer.
* @param {number} value
* @private
*/
writeUnsignedVarint32_(value) {
checkTypeUnsignedInt32(value);
while (value > 0x7f) {
this.currentBuffer_.push((value & 0x7f) | 0x80);
value = value >>> 7;
}
this.currentBuffer_.push(value);
}
/****************************************************************************
* OPTIONAL METHODS
****************************************************************************/
/**
* Writes a boolean value field to the buffer as a varint.
* @param {boolean} value
* @private
*/
writeBoolValue_(value) {
this.currentBuffer_.push(value ? 1 : 0);
}
/**
* Writes a boolean value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {boolean} value
*/
writeBool(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeBoolValue_(value);
}
/**
* Writes a bytes value field to the buffer as a length delimited field.
* @param {number} fieldNumber
* @param {!ByteString} value
*/
writeBytes(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.DELIMITED);
const buffer = value.toArrayBuffer();
this.writeUnsignedVarint32_(buffer.byteLength);
this.writeRaw_(buffer);
}
/**
* Writes a double value field to the buffer without tag.
* @param {number} value
* @private
*/
writeDoubleValue_(value) {
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setFloat64(0, value, true);
this.writeRaw_(buffer);
}
/**
* Writes a double value field to the buffer.
* @param {number} fieldNumber
* @param {number} value
*/
writeDouble(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.FIXED64);
this.writeDoubleValue_(value);
}
/**
* Writes a fixed32 value field to the buffer without tag.
* @param {number} value
* @private
*/
writeFixed32Value_(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setUint32(0, value, true);
this.writeRaw_(buffer);
}
/**
* Writes a fixed32 value field to the buffer.
* @param {number} fieldNumber
* @param {number} value
*/
writeFixed32(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.FIXED32);
this.writeFixed32Value_(value);
}
/**
* Writes a float value field to the buffer without tag.
* @param {number} value
* @private
*/
writeFloatValue_(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setFloat32(0, value, true);
this.writeRaw_(buffer);
}
/**
* Writes a float value field to the buffer.
* @param {number} fieldNumber
* @param {number} value
*/
writeFloat(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.FIXED32);
this.writeFloatValue_(value);
}
/**
* Writes a int32 value field to the buffer as a varint without tag.
* @param {number} value
* @private
*/
writeInt32Value_(value) {
if (value >= 0) {
this.writeVarint64_(0, value);
} else {
this.writeVarint64_(0xFFFFFFFF, value);
}
}
/**
* Writes a int32 value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {number} value
*/
writeInt32(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeInt32Value_(value);
}
/**
* Writes a int64 value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {!Int64} value
*/
writeInt64(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeVarint64_(value.getHighBits(), value.getLowBits());
}
/**
* Writes a sfixed32 value field to the buffer.
* @param {number} value
* @private
*/
writeSfixed32Value_(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setInt32(0, value, true);
this.writeRaw_(buffer);
}
/**
* Writes a sfixed32 value field to the buffer.
* @param {number} fieldNumber
* @param {number} value
*/
writeSfixed32(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.FIXED32);
this.writeSfixed32Value_(value);
}
/**
* Writes a sfixed64 value field to the buffer without tag.
* @param {!Int64} value
* @private
*/
writeSfixed64Value_(value) {
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setInt32(0, value.getLowBits(), true);
view.setInt32(4, value.getHighBits(), true);
this.writeRaw_(buffer);
}
/**
* Writes a sfixed64 value field to the buffer.
* @param {number} fieldNumber
* @param {!Int64} value
*/
writeSfixed64(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.FIXED64);
this.writeSfixed64Value_(value);
}
/**
* Writes a sfixed64 value field to the buffer.
* @param {number} fieldNumber
*/
writeStartGroup(fieldNumber) {
this.writeTag(fieldNumber, WireType.START_GROUP);
}
/**
* Writes a sfixed64 value field to the buffer.
* @param {number} fieldNumber
*/
writeEndGroup(fieldNumber) {
this.writeTag(fieldNumber, WireType.END_GROUP);
}
/**
* Writes a uint32 value field to the buffer as a varint without tag.
* @param {number} value
* @private
*/
writeUint32Value_(value) {
this.writeVarint64_(0, value);
}
/**
* Writes a uint32 value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {number} value
*/
writeUint32(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeUint32Value_(value);
}
/**
* Writes the bits of a 64 bit number to the buffer as a varint.
* @param {number} highBits
* @param {number} lowBits
* @private
*/
writeVarint64_(highBits, lowBits) {
for (let i = 0; i < 28; i = i + 7) {
const shift = lowBits >>> i;
const hasNext = !((shift >>> 7) === 0 && highBits === 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
this.currentBuffer_.push(byte);
if (!hasNext) {
return;
}
}
const splitBits = ((lowBits >>> 28) & 0x0F) | ((highBits & 0x07) << 4);
const hasMoreBits = !((highBits >> 3) === 0);
this.currentBuffer_.push(
(hasMoreBits ? splitBits | 0x80 : splitBits) & 0xFF);
if (!hasMoreBits) {
return;
}
for (let i = 3; i < 31; i = i + 7) {
const shift = highBits >>> i;
const hasNext = !((shift >>> 7) === 0);
const byte = (hasNext ? shift | 0x80 : shift) & 0xFF;
this.currentBuffer_.push(byte);
if (!hasNext) {
return;
}
}
this.currentBuffer_.push((highBits >>> 31) & 0x01);
}
/**
* Writes a sint32 value field to the buffer as a varint without tag.
* @param {number} value
* @private
*/
writeSint32Value_(value) {
value = (value << 1) ^ (value >> 31);
this.writeVarint64_(0, value);
}
/**
* Writes a sint32 value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {number} value
*/
writeSint32(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeSint32Value_(value);
}
/**
* Writes a sint64 value field to the buffer as a varint without tag.
* @param {!Int64} value
* @private
*/
writeSint64Value_(value) {
const highBits = value.getHighBits();
const lowBits = value.getLowBits();
const sign = highBits >> 31;
const encodedLowBits = (lowBits << 1) ^ sign;
const encodedHighBits = ((highBits << 1) | (lowBits >>> 31)) ^ sign;
this.writeVarint64_(encodedHighBits, encodedLowBits);
}
/**
* Writes a sint64 value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {!Int64} value
*/
writeSint64(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.VARINT);
this.writeSint64Value_(value);
}
/**
* Writes a string value field to the buffer as a varint.
* @param {number} fieldNumber
* @param {string} value
*/
writeString(fieldNumber, value) {
this.writeTag(fieldNumber, WireType.DELIMITED);
const array = encoderFunction(value);
this.writeUnsignedVarint32_(array.length);
this.closeAndStartNewBuffer_();
this.blocks_.push(array);
}
/**
* Writes raw bytes to the buffer.
* @param {!ArrayBuffer} arrayBuffer
* @private
*/
writeRaw_(arrayBuffer) {
this.closeAndStartNewBuffer_();
this.blocks_.push(new Uint8Array(arrayBuffer));
}
/**
* Writes raw bytes to the buffer.
* @param {!BufferDecoder} bufferDecoder
* @param {number} start
* @param {!WireType} wireType
* @param {number} fieldNumber
* @package
*/
writeBufferDecoder(bufferDecoder, start, wireType, fieldNumber) {
this.closeAndStartNewBuffer_();
const dataLength =
getTagLength(bufferDecoder, start, wireType, fieldNumber);
this.blocks_.push(
bufferDecoder.subBufferDecoder(start, dataLength).asUint8Array());
}
/**
* Write the whole bytes as a length delimited field.
* @param {number} fieldNumber
* @param {!ArrayBuffer} arrayBuffer
*/
writeDelimited(fieldNumber, arrayBuffer) {
this.writeTag(fieldNumber, WireType.DELIMITED);
this.writeUnsignedVarint32_(arrayBuffer.byteLength);
this.writeRaw_(arrayBuffer);
}
/****************************************************************************
* REPEATED METHODS
****************************************************************************/
/**
* Writes repeated boolean values to the buffer as unpacked varints.
* @param {number} fieldNumber
* @param {!Array<boolean>} values
*/
writeRepeatedBool(fieldNumber, values) {
values.forEach(val => this.writeBool(fieldNumber, val));
}
/**
* Writes repeated boolean values to the buffer as packed varints.
* @param {number} fieldNumber
* @param {!Array<boolean>} values
*/
writePackedBool(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeBoolValue_(val), 1);
}
/**
* Writes repeated double values to the buffer as unpacked fixed64.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedDouble(fieldNumber, values) {
values.forEach(val => this.writeDouble(fieldNumber, val));
}
/**
* Writes repeated double values to the buffer as packed fixed64.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedDouble(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeDoubleValue_(val), 8);
}
/**
* Writes repeated fixed32 values to the buffer as unpacked fixed32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedFixed32(fieldNumber, values) {
values.forEach(val => this.writeFixed32(fieldNumber, val));
}
/**
* Writes repeated fixed32 values to the buffer as packed fixed32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedFixed32(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeFixed32Value_(val), 4);
}
/**
* Writes repeated float values to the buffer as unpacked fixed64.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedFloat(fieldNumber, values) {
values.forEach(val => this.writeFloat(fieldNumber, val));
}
/**
* Writes repeated float values to the buffer as packed fixed64.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedFloat(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeFloatValue_(val), 4);
}
/**
* Writes repeated int32 values to the buffer as unpacked int32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedInt32(fieldNumber, values) {
values.forEach(val => this.writeInt32(fieldNumber, val));
}
/**
* Writes repeated int32 values to the buffer as packed int32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedInt32(fieldNumber, values) {
this.writeVariablePacked_(
fieldNumber, values, (writer, val) => writer.writeInt32Value_(val));
}
/**
* Writes repeated int64 values to the buffer as unpacked varint.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writeRepeatedInt64(fieldNumber, values) {
values.forEach(val => this.writeInt64(fieldNumber, val));
}
/**
* Writes repeated int64 values to the buffer as packed varint.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writePackedInt64(fieldNumber, values) {
this.writeVariablePacked_(
fieldNumber, values,
(writer, val) =>
writer.writeVarint64_(val.getHighBits(), val.getLowBits()));
}
/**
* Writes repeated sfixed32 values to the buffer as unpacked fixed32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedSfixed32(fieldNumber, values) {
values.forEach(val => this.writeSfixed32(fieldNumber, val));
}
/**
* Writes repeated sfixed32 values to the buffer as packed fixed32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedSfixed32(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeSfixed32Value_(val), 4);
}
/**
* Writes repeated sfixed64 values to the buffer as unpacked fixed64.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writeRepeatedSfixed64(fieldNumber, values) {
values.forEach(val => this.writeSfixed64(fieldNumber, val));
}
/**
* Writes repeated sfixed64 values to the buffer as packed fixed64.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writePackedSfixed64(fieldNumber, values) {
this.writeFixedPacked_(
fieldNumber, values, val => this.writeSfixed64Value_(val), 8);
}
/**
* Writes repeated sint32 values to the buffer as unpacked sint32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedSint32(fieldNumber, values) {
values.forEach(val => this.writeSint32(fieldNumber, val));
}
/**
* Writes repeated sint32 values to the buffer as packed sint32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedSint32(fieldNumber, values) {
this.writeVariablePacked_(
fieldNumber, values, (writer, val) => writer.writeSint32Value_(val));
}
/**
* Writes repeated sint64 values to the buffer as unpacked varint.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writeRepeatedSint64(fieldNumber, values) {
values.forEach(val => this.writeSint64(fieldNumber, val));
}
/**
* Writes repeated sint64 values to the buffer as packed varint.
* @param {number} fieldNumber
* @param {!Array<!Int64>} values
*/
writePackedSint64(fieldNumber, values) {
this.writeVariablePacked_(
fieldNumber, values, (writer, val) => writer.writeSint64Value_(val));
}
/**
* Writes repeated uint32 values to the buffer as unpacked uint32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writeRepeatedUint32(fieldNumber, values) {
values.forEach(val => this.writeUint32(fieldNumber, val));
}
/**
* Writes repeated uint32 values to the buffer as packed uint32.
* @param {number} fieldNumber
* @param {!Array<number>} values
*/
writePackedUint32(fieldNumber, values) {
this.writeVariablePacked_(
fieldNumber, values, (writer, val) => writer.writeUint32Value_(val));
}
/**
* Writes repeated bytes values to the buffer.
* @param {number} fieldNumber
* @param {!Array<!ByteString>} values
*/
writeRepeatedBytes(fieldNumber, values) {
values.forEach(val => this.writeBytes(fieldNumber, val));
}
/**
* Writes packed fields with fixed length.
* @param {number} fieldNumber
* @param {!Array<T>} values
* @param {function(T)} valueWriter
* @param {number} entitySize
* @template T
* @private
*/
writeFixedPacked_(fieldNumber, values, valueWriter, entitySize) {
if (values.length === 0) {
return;
}
this.writeTag(fieldNumber, WireType.DELIMITED);
this.writeUnsignedVarint32_(values.length * entitySize);
this.closeAndStartNewBuffer_();
values.forEach(value => valueWriter(value));
}
/**
* Writes packed fields with variable length.
* @param {number} fieldNumber
* @param {!Array<T>} values
* @param {function(!Writer, T)} valueWriter
* @template T
* @private
*/
writeVariablePacked_(fieldNumber, values, valueWriter) {
if (values.length === 0) {
return;
}
const writer = new Writer();
values.forEach(val => valueWriter(writer, val));
const bytes = writer.getAndResetResultBuffer();
this.writeDelimited(fieldNumber, bytes);
}
/**
* Writes repeated string values to the buffer.
* @param {number} fieldNumber
* @param {!Array<string>} values
*/
writeRepeatedString(fieldNumber, values) {
values.forEach(val => this.writeString(fieldNumber, val));
}
}
exports = Writer;