1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.deserializeBinary = exports.serializeBinary = exports.TAG_MAP_SERIALIZED_SIZE_LIMIT = void 0;
|
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 | const tag_map_1 = require("../tag-map");
|
49 | const variant_encoding_1 = require("./variant-encoding");
|
50 |
|
51 | exports.TAG_MAP_SERIALIZED_SIZE_LIMIT = 8192;
|
52 | const ENCODING = 'utf8';
|
53 | const VERSION_ID = 0;
|
54 | const TAG_FIELD_ID = 0;
|
55 | const VERSION_ID_INDEX = 0;
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function serializeBinary(tagMap) {
|
61 | const byteArray = [];
|
62 | byteArray.push(VERSION_ID);
|
63 | let totalChars = 0;
|
64 | const tags = tagMap.tags;
|
65 | tags.forEach((tagValue, tagKey) => {
|
66 | totalChars += tagKey.name.length;
|
67 | totalChars += tagValue.value.length;
|
68 | encodeTag(tagKey, tagValue, byteArray);
|
69 | });
|
70 | if (totalChars > exports.TAG_MAP_SERIALIZED_SIZE_LIMIT) {
|
71 | throw new Error(`Size of TagMap exceeds the maximum serialized size ${exports.TAG_MAP_SERIALIZED_SIZE_LIMIT}`);
|
72 | }
|
73 | return Buffer.from(byteArray);
|
74 | }
|
75 | exports.serializeBinary = serializeBinary;
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function deserializeBinary(buffer) {
|
81 | if (buffer.length === 0) {
|
82 | throw new Error('Input buffer can not be empty.');
|
83 | }
|
84 | const versionId = buffer.readInt8(VERSION_ID_INDEX);
|
85 | if (versionId > VERSION_ID) {
|
86 | throw new Error(`Wrong Version ID: ${versionId}. Currently supports version up to: ${VERSION_ID}`);
|
87 | }
|
88 | return parseTags(buffer);
|
89 | }
|
90 | exports.deserializeBinary = deserializeBinary;
|
91 | function encodeTag(tagKey, tagValue, byteArray) {
|
92 | byteArray.push(TAG_FIELD_ID);
|
93 | encodeString(tagKey.name, byteArray);
|
94 | encodeString(tagValue.value, byteArray);
|
95 | }
|
96 | function encodeString(input, byteArray) {
|
97 | byteArray.push(...variant_encoding_1.EncodeVarint(input.length));
|
98 | byteArray.push(...input.split('').map(unicode));
|
99 | return byteArray;
|
100 | }
|
101 | function parseTags(buffer) {
|
102 | const tags = new tag_map_1.TagMap();
|
103 | const limit = buffer.length;
|
104 | let totalChars = 0;
|
105 | let currentIndex = 1;
|
106 | while (currentIndex < limit) {
|
107 | const fieldId = buffer.readInt8(currentIndex);
|
108 | if (fieldId > TAG_FIELD_ID) {
|
109 |
|
110 |
|
111 | break;
|
112 | }
|
113 | currentIndex += 1;
|
114 | const key = decodeString(buffer, currentIndex);
|
115 | currentIndex += key.length;
|
116 | totalChars += key.length;
|
117 | currentIndex += 1;
|
118 | const val = decodeString(buffer, currentIndex);
|
119 | currentIndex += val.length;
|
120 | totalChars += val.length;
|
121 | currentIndex += 1;
|
122 | if (totalChars > exports.TAG_MAP_SERIALIZED_SIZE_LIMIT) {
|
123 | throw new Error(`Size of TagMap exceeds the maximum serialized size ${exports.TAG_MAP_SERIALIZED_SIZE_LIMIT}`);
|
124 | }
|
125 | else {
|
126 | tags.set({ name: key }, { value: val });
|
127 | }
|
128 | }
|
129 | return tags;
|
130 | }
|
131 | function decodeString(buffer, offset) {
|
132 | const length = variant_encoding_1.DecodeVarint(buffer, offset);
|
133 | return buffer.toString(ENCODING, offset + 1, offset + 1 + length);
|
134 | }
|
135 | function unicode(x) {
|
136 | return x.charCodeAt(0);
|
137 | }
|
138 |
|
\ | No newline at end of file |