/
writer.js
644 lines (578 loc) · 17.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
"use strict";
module.exports = Writer;
Writer.BufferWriter = BufferWriter;
var util = require("./util/runtime"),
ieee754 = require("../lib/ieee754");
var LongBits = util.LongBits,
ArrayImpl = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
/**
* Constructs a new writer operation.
* @classdesc Scheduled writer operation.
* @memberof Writer
* @constructor
* @param {function(Uint8Array, number, *)} fn Function to call
* @param {*} val Value to write
* @param {number} len Value byte length
* @private
* @ignore
*/
function Op(fn, val, len) {
/**
* Function to call.
* @type {function(Uint8Array, number, *)}
*/
this.fn = fn;
/**
* Value to write.
* @type {*}
*/
this.val = val;
/**
* Value byte length.
* @type {number}
*/
this.len = len;
/**
* Next operation.
* @type {?Writer.Op}
*/
this.next = null;
}
Writer.Op = Op;
function noop() {} // eslint-disable-line no-empty-function
/**
* Constructs a new writer state.
* @classdesc Copied writer state.
* @memberof Writer
* @constructor
* @param {Writer} writer Writer to copy state from
* @param {State} next Next state entry
* @private
* @ignore
*/
function State(writer, next) {
/**
* Current head.
* @type {Writer.Op}
*/
this.head = writer.head;
/**
* Current tail.
* @type {Writer.Op}
*/
this.tail = writer.tail;
/**
* Current buffer length.
* @type {number}
*/
this.len = writer.len;
/**
* Next state.
* @type {?State}
*/
this.next = next;
}
Writer.State = State;
/**
* Constructs a new writer.
* @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
* @constructor
*/
function Writer() {
/**
* Current length.
* @type {number}
*/
this.len = 0;
/**
* Operations head.
* @type {Object}
*/
this.head = new Op(noop, 0, 0);
/**
* Operations tail
* @type {Object}
*/
this.tail = this.head;
/**
* Linked forked states.
* @type {?Object}
*/
this.states = null;
// When a value is written, the writer calculates its byte length and puts it into a linked
// list of operations to perform when finish() is called. This both allows us to allocate
// buffers of the exact required size and reduces the amount of work we have to do compared
// to first calculating over objects and then encoding over objects. In our case, the encoding
// part is just a linked list walk calling linked operations with already prepared values.
}
/**
* Creates a new writer.
* @returns {BufferWriter|Writer} A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
*/
Writer.create = function create() {
return new (util.Buffer && BufferWriter || Writer);
};
/**
* Allocates a buffer of the specified size.
* @param {number} size Buffer size
* @returns {Uint8Array} Buffer
*/
Writer.alloc = function alloc(size) {
return new ArrayImpl(size);
};
// Use Uint8Array buffer pool in the browser, just like node does with buffers
if (ArrayImpl !== Array)
Writer.alloc = util.pool(Writer.alloc, ArrayImpl.prototype.subarray || ArrayImpl.prototype.slice);
/** @alias Writer.prototype */
var WriterPrototype = Writer.prototype;
/**
* Pushes a new operation to the queue.
* @param {function(Uint8Array, number, *)} fn Function to call
* @param {number} len Value byte length
* @param {number} val Value to write
* @returns {Writer} `this`
*/
WriterPrototype.push = function push(fn, len, val) {
var op = new Op(fn, val, len);
this.tail.next = op;
this.tail = op;
this.len += len;
return this;
};
function writeByte(buf, pos, val) {
buf[pos] = val & 255;
}
/**
* Writes a tag.
* @param {number} id Field id
* @param {number} wireType Wire type
* @returns {Writer} `this`
*/
WriterPrototype.tag = function write_tag(id, wireType) {
return this.push(writeByte, 1, id << 3 | wireType & 7);
};
function writeVarint32(buf, pos, val) {
while (val > 127) {
buf[pos++] = val & 127 | 128;
val >>>= 7;
}
buf[pos] = val;
}
/**
* Writes an unsigned 32 bit value as a varint.
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.uint32 = function write_uint32(value) {
value >>>= 0;
return value < 128
? this.push(writeByte, 1, value)
: this.push(writeVarint32,
value < 16384 ? 2
: value < 2097152 ? 3
: value < 268435456 ? 4
: 5
, value);
};
/**
* Writes a signed 32 bit value as a varint.
* @function
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.int32 = function write_int32(value) {
return value < 0
? this.push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
: this.uint32(value);
};
/**
* Writes a 32 bit value as a varint, zig-zag encoded.
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.sint32 = function write_sint32(value) {
return this.uint32(value << 1 ^ value >> 31);
};
function writeVarint64(buf, pos, val) {
// tends to deoptimize. stays optimized when using bits directly.
while (val.hi) {
buf[pos++] = val.lo & 127 | 128;
val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
val.hi >>>= 7;
}
while (val.lo > 127) {
buf[pos++] = val.lo & 127 | 128;
val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
}
buf[pos++] = val.lo;
}
/**
* Writes an unsigned 64 bit value as a varint.
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
* @throws {TypeError} If `value` is a string and no long library is present.
*/
WriterPrototype.uint64 = function write_uint64(value) {
var bits = LongBits.from(value);
return this.push(writeVarint64, bits.length(), bits);
};
/**
* Writes a signed 64 bit value as a varint.
* @function
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
* @throws {TypeError} If `value` is a string and no long library is present.
*/
WriterPrototype.int64 = WriterPrototype.uint64;
/**
* Writes a signed 64 bit value as a varint, zig-zag encoded.
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
* @throws {TypeError} If `value` is a string and no long library is present.
*/
WriterPrototype.sint64 = function write_sint64(value) {
var bits = LongBits.from(value).zzEncode();
return this.push(writeVarint64, bits.length(), bits);
};
/**
* Writes a boolish value as a varint.
* @param {boolean} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.bool = function write_bool(value) {
return this.push(writeByte, 1, value ? 1 : 0);
};
function writeFixed32(buf, pos, val) {
buf[pos++] = val & 255;
buf[pos++] = val >>> 8 & 255;
buf[pos++] = val >>> 16 & 255;
buf[pos ] = val >>> 24;
}
/**
* Writes a 32 bit value as fixed 32 bits.
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.fixed32 = function write_fixed32(value) {
return this.push(writeFixed32, 4, value >>> 0);
};
/**
* Writes a 32 bit value as fixed 32 bits, zig-zag encoded.
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.sfixed32 = function write_sfixed32(value) {
return this.push(writeFixed32, 4, value << 1 ^ value >> 31);
};
/**
* Writes a 64 bit value as fixed 64 bits.
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
* @throws {TypeError} If `value` is a string and no long library is present.
*/
WriterPrototype.fixed64 = function write_fixed64(value) {
var bits = LongBits.from(value);
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
};
/**
* Writes a 64 bit value as fixed 64 bits, zig-zag encoded.
* @param {Long|number|string} value Value to write
* @returns {Writer} `this`
* @throws {TypeError} If `value` is a string and no long library is present.
*/
WriterPrototype.sfixed64 = function write_sfixed64(value) {
var bits = LongBits.from(value).zzEncode();
return this.push(writeFixed32, 4, bits.hi).push(writeFixed32, 4, bits.lo);
};
var writeFloat = typeof Float32Array !== 'undefined'
? (function() { // eslint-disable-line wrap-iife
var f32 = new Float32Array(1),
f8b = new Uint8Array(f32.buffer);
f32[0] = -0;
return f8b[3] // already le?
? function writeFloat_f32(buf, pos, val) {
f32[0] = val;
buf[pos++] = f8b[0];
buf[pos++] = f8b[1];
buf[pos++] = f8b[2];
buf[pos ] = f8b[3];
}
: function writeFloat_f32_le(buf, pos, val) {
f32[0] = val;
buf[pos++] = f8b[3];
buf[pos++] = f8b[2];
buf[pos++] = f8b[1];
buf[pos ] = f8b[0];
};
})()
: function writeFloat_ieee754(buf, pos, val) {
ieee754.write(buf, val, pos, false, 23, 4);
};
/**
* Writes a float (32 bit).
* @function
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.float = function write_float(value) {
return this.push(writeFloat, 4, value);
};
var writeDouble = typeof Float64Array !== 'undefined'
? (function() { // eslint-disable-line wrap-iife
var f64 = new Float64Array(1),
f8b = new Uint8Array(f64.buffer);
f64[0] = -0;
return f8b[7] // already le?
? function writeDouble_f64(buf, pos, val) {
f64[0] = val;
buf[pos++] = f8b[0];
buf[pos++] = f8b[1];
buf[pos++] = f8b[2];
buf[pos++] = f8b[3];
buf[pos++] = f8b[4];
buf[pos++] = f8b[5];
buf[pos++] = f8b[6];
buf[pos ] = f8b[7];
}
: function writeDouble_f64_le(buf, pos, val) {
f64[0] = val;
buf[pos++] = f8b[7];
buf[pos++] = f8b[6];
buf[pos++] = f8b[5];
buf[pos++] = f8b[4];
buf[pos++] = f8b[3];
buf[pos++] = f8b[2];
buf[pos++] = f8b[1];
buf[pos ] = f8b[0];
};
})()
: function writeDouble_ieee754(buf, pos, val) {
ieee754.write(buf, val, pos, false, 52, 8);
};
/**
* Writes a double (64 bit float).
* @function
* @param {number} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.double = function write_double(value) {
return this.push(writeDouble, 8, value);
};
var writeBytes = ArrayImpl.prototype.set
? function writeBytes_set(buf, pos, val) {
buf.set(val, pos);
}
: function writeBytes_for(buf, pos, val) {
for (var i = 0; i < val.length; ++i)
buf[pos + i] = val[i];
};
/**
* Writes a sequence of bytes.
* @param {Uint8Array} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.bytes = function write_bytes(value) {
var len = value.length >>> 0;
return len
? this.uint32(len).push(writeBytes, len, value)
: this.push(writeByte, 1, 0);
};
function writeString(buf, pos, val) {
for (var i = 0; i < val.length; ++i) {
var c1 = val.charCodeAt(i), c2;
if (c1 < 128) {
buf[pos++] = c1;
} else if (c1 < 2048) {
buf[pos++] = c1 >> 6 | 192;
buf[pos++] = c1 & 63 | 128;
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = val.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
++i;
buf[pos++] = c1 >> 18 | 240;
buf[pos++] = c1 >> 12 & 63 | 128;
buf[pos++] = c1 >> 6 & 63 | 128;
buf[pos++] = c1 & 63 | 128;
} else {
buf[pos++] = c1 >> 12 | 224;
buf[pos++] = c1 >> 6 & 63 | 128;
buf[pos++] = c1 & 63 | 128;
}
}
}
function byteLength(val) {
var strlen = val.length >>> 0;
var len = 0;
for (var i = 0; i < strlen; ++i) {
var c1 = val.charCodeAt(i);
if (c1 < 128)
len += 1;
else if (c1 < 2048)
len += 2;
else if ((c1 & 0xFC00) === 0xD800 && (val.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
++i;
len += 4;
} else
len += 3;
}
return len;
}
/**
* Writes a string.
* @param {string} value Value to write
* @returns {Writer} `this`
*/
WriterPrototype.string = function write_string(value) {
var len = byteLength(value);
return len
? this.uint32(len).push(writeString, len, value)
: this.push(writeByte, 1, 0);
};
/**
* Forks this writer's state by pushing it to a stack.
* Calling {@link Writer#ldelim}, {@link Writer#reset} or {@link Writer#finish} resets the writer to the previous state.
* @returns {Writer} `this`
*/
WriterPrototype.fork = function fork() {
this.states = new State(this, this.states);
this.head = this.tail = new Op(noop, 0, 0);
this.len = 0;
return this;
};
/**
* Resets this instance to the last state.
* @returns {Writer} `this`
*/
WriterPrototype.reset = function reset() {
if (this.states) {
this.head = this.states.head;
this.tail = this.states.tail;
this.len = this.states.len;
this.states = this.states.next;
} else {
this.head = this.tail = new Op(noop, 0, 0);
this.len = 0;
}
return this;
};
/**
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
* @param {number} [id] Id with wire type 2 to prepend where applicable
* @returns {Writer} `this`
*/
WriterPrototype.ldelim = function ldelim(id) {
var head = this.head,
tail = this.tail,
len = this.len;
this.reset();
if (id !== undefined)
this.tag(id, 2);
this.uint32(len);
this.tail.next = head.next; // skip noop
this.tail = tail;
this.len += len;
return this;
};
/**
* Finishes the current sequence of write operations and frees all resources.
* @returns {Uint8Array} Finished buffer
*/
WriterPrototype.finish = function finish() {
var head = this.head.next, // skip noop
buf = this.constructor.alloc(this.len);
this.reset();
var pos = 0;
while (head) {
head.fn(buf, pos, head.val);
pos += head.len;
head = head.next;
}
return buf;
};
/**
* Constructs a new buffer writer.
* @classdesc Wire format writer using node buffers.
* @exports BufferWriter
* @extends Writer
* @constructor
*/
function BufferWriter() {
Writer.call(this);
}
/**
* Allocates a buffer of the specified size.
* @param {number} size Buffer size
* @returns {Uint8Array} Buffer
*/
BufferWriter.alloc = function alloc_buffer(size) {
BufferWriter.alloc = util.Buffer.allocUnsafe
? util.Buffer.allocUnsafe
: function allocUnsafeNew(size) { return new util.Buffer(size); };
return BufferWriter.alloc(size);
};
/** @alias BufferWriter.prototype */
var BufferWriterPrototype = BufferWriter.prototype = Object.create(Writer.prototype);
BufferWriterPrototype.constructor = BufferWriter;
function writeFloatBuffer(buf, pos, val) {
buf.writeFloatLE(val, pos, true);
}
if (typeof Float32Array === 'undefined') // f32 is faster (node 6.9.1)
/**
* @override
*/
BufferWriterPrototype.float = function write_float_buffer(value) {
return this.push(writeFloatBuffer, 4, value);
};
function writeDoubleBuffer(buf, pos, val) {
buf.writeDoubleLE(val, pos, true);
}
if (typeof Float64Array === 'undefined') // f64 is faster (node 6.9.1)
/**
* @override
*/
BufferWriterPrototype.double = function write_double_buffer(value) {
return this.push(writeDoubleBuffer, 8, value);
};
function writeBytesBuffer(buf, pos, val) {
if (val.length)
val.copy(buf, pos, 0, val.length);
// This could probably be optimized just like writeStringBuffer, but most real use cases won't benefit much.
}
if (!(ArrayImpl.prototype.set && util.Buffer && util.Buffer.prototype.set)) // set is faster (node 6.9.1)
/**
* @override
*/
BufferWriterPrototype.bytes = function write_bytes_buffer(value) {
var len = value.length >>> 0;
return len
? this.uint32(len).push(writeBytesBuffer, len, value)
: this.push(writeByte, 1, 0);
};
var writeStringBuffer = (function() { // eslint-disable-line wrap-iife
return util.Buffer && util.Buffer.prototype.utf8Write // around forever, but not present in browser buffer
? function writeString_buffer_utf8Write(buf, pos, val) {
if (val.length < 40)
writeString(buf, pos, val);
else
buf.utf8Write(val, pos);
}
: function writeString_buffer_write(buf, pos, val) {
if (val.length < 40)
writeString(buf, pos, val);
else
buf.write(val, pos);
};
// Note that the plain JS encoder is faster for short strings, probably because of redundant assertions.
// For a raw utf8Write, the breaking point is about 20 characters, for write it is around 40 characters.
// Unfortunately, this does not translate 1:1 to real use cases, hence the common "good enough" limit of 40.
})();
/**
* @override
*/
BufferWriterPrototype.string = function write_string_buffer(value) {
var len = value.length < 40
? byteLength(value)
: util.Buffer.byteLength(value);
return len
? this.uint32(len).push(writeStringBuffer, len, value)
: this.push(writeByte, 1, 0);
};