stage-browser 0.1.0 → 0.2.0
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/README.md +92 -45
- package/build/icon.icns +0 -0
- package/build/icon.png +0 -0
- package/dist/bin/stage.js +111 -0
- package/dist/main/cli-args.js +28 -0
- package/dist/main/context-menu.js +68 -0
- package/dist/main/context.js +2 -0
- package/dist/main/extensions.js +78 -0
- package/dist/main/ipc.js +73 -0
- package/dist/main/main.js +282 -0
- package/dist/main/menu.js +140 -0
- package/dist/main/reveal.js +170 -0
- package/dist/main/selftest.js +570 -0
- package/dist/main/store.js +85 -0
- package/dist/main/suggest.js +43 -0
- package/dist/main/tabs.js +324 -0
- package/dist/main/ui-window.js +133 -0
- package/dist/main/url.js +38 -0
- package/dist/main/window.js +133 -0
- package/dist/preload/extension-compat.js +163 -0
- package/dist/preload/ui-preload.js +34 -0
- package/dist/shared/contract.js +6 -0
- package/dist/ui/app.js +692 -0
- package/{src → dist}/ui/index.html +15 -5
- package/dist/ui/mock.js +500 -0
- package/{src → dist}/ui/styles.css +55 -14
- package/node_modules/@types/chrome/LICENSE +21 -0
- package/node_modules/@types/chrome/README.md +15 -0
- package/node_modules/@types/chrome/chrome-cast/index.d.ts +1156 -0
- package/node_modules/@types/chrome/har-format/index.d.ts +6 -0
- package/node_modules/@types/chrome/index.d.ts +13786 -0
- package/node_modules/@types/chrome/package.json +99 -0
- package/node_modules/@types/filesystem/LICENSE +21 -0
- package/node_modules/@types/filesystem/README.md +15 -0
- package/node_modules/@types/filesystem/index.d.ts +564 -0
- package/node_modules/@types/filesystem/package.json +26 -0
- package/node_modules/@types/filewriter/LICENSE +21 -0
- package/node_modules/@types/filewriter/README.md +15 -0
- package/node_modules/@types/filewriter/index.d.ts +166 -0
- package/node_modules/@types/filewriter/package.json +25 -0
- package/node_modules/@types/har-format/LICENSE +21 -0
- package/node_modules/@types/har-format/README.md +15 -0
- package/node_modules/@types/har-format/index.d.ts +938 -0
- package/node_modules/@types/har-format/package.json +31 -0
- package/node_modules/adm-zip/LICENSE +21 -0
- package/node_modules/adm-zip/README.md +90 -0
- package/node_modules/adm-zip/adm-zip.js +1020 -0
- package/node_modules/adm-zip/headers/entryHeader.js +396 -0
- package/node_modules/adm-zip/headers/index.js +2 -0
- package/node_modules/adm-zip/headers/mainHeader.js +177 -0
- package/node_modules/adm-zip/methods/deflater.js +33 -0
- package/node_modules/adm-zip/methods/index.js +3 -0
- package/node_modules/adm-zip/methods/inflater.js +64 -0
- package/node_modules/adm-zip/methods/zipcrypto.js +175 -0
- package/node_modules/adm-zip/package.json +56 -0
- package/node_modules/adm-zip/types.d.ts +223 -0
- package/node_modules/adm-zip/util/constants.js +142 -0
- package/node_modules/adm-zip/util/decoder.js +5 -0
- package/node_modules/adm-zip/util/errors.js +66 -0
- package/node_modules/adm-zip/util/fattr.js +76 -0
- package/node_modules/adm-zip/util/index.js +5 -0
- package/node_modules/adm-zip/util/utils.js +481 -0
- package/node_modules/adm-zip/zipEntry.js +415 -0
- package/node_modules/adm-zip/zipFile.js +466 -0
- package/node_modules/debug/LICENSE +20 -0
- package/node_modules/debug/README.md +481 -0
- package/node_modules/debug/package.json +64 -0
- package/node_modules/debug/src/browser.js +272 -0
- package/node_modules/debug/src/common.js +292 -0
- package/node_modules/debug/src/index.js +10 -0
- package/node_modules/debug/src/node.js +263 -0
- package/node_modules/electron-chrome-web-store/README.md +143 -0
- package/node_modules/electron-chrome-web-store/dist/chrome-web-store.preload.js +299 -0
- package/node_modules/electron-chrome-web-store/dist/cjs/browser/index.js +1674 -0
- package/node_modules/electron-chrome-web-store/dist/esm/browser/index.mjs +950 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/api.d.ts +2 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/crx3.d.ts +20 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/id.d.ts +9 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/index.d.ts +62 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/installer.d.ts +29 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/loader.d.ts +18 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/types.d.ts +21 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/updater.d.ts +6 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/utils.d.ts +4 -0
- package/node_modules/electron-chrome-web-store/dist/types/common/constants.d.ts +44 -0
- package/node_modules/electron-chrome-web-store/dist/types/renderer/chrome-web-store.preload.d.ts +1 -0
- package/node_modules/electron-chrome-web-store/package.json +43 -0
- package/node_modules/ms/index.js +162 -0
- package/node_modules/ms/license.md +21 -0
- package/node_modules/ms/package.json +38 -0
- package/node_modules/ms/readme.md +59 -0
- package/node_modules/pbf/LICENSE +27 -0
- package/node_modules/pbf/README.md +293 -0
- package/node_modules/pbf/bin/pbf +17 -0
- package/node_modules/pbf/compile.js +441 -0
- package/node_modules/pbf/dist/pbf-dev.js +838 -0
- package/node_modules/pbf/dist/pbf.js +1 -0
- package/node_modules/pbf/index.d.ts +206 -0
- package/node_modules/pbf/index.js +830 -0
- package/node_modules/pbf/package.json +63 -0
- package/node_modules/protocol-buffers-schema/.travis.yml +7 -0
- package/node_modules/protocol-buffers-schema/LICENSE +21 -0
- package/node_modules/protocol-buffers-schema/README.md +122 -0
- package/node_modules/protocol-buffers-schema/example.js +11 -0
- package/node_modules/protocol-buffers-schema/example.proto +13 -0
- package/node_modules/protocol-buffers-schema/index.js +6 -0
- package/node_modules/protocol-buffers-schema/package.json +31 -0
- package/node_modules/protocol-buffers-schema/parse.js +769 -0
- package/node_modules/protocol-buffers-schema/stringify.js +206 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.proto +11 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.json +99 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.json +125 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.json +42 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.json +32 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.json +167 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.json +33 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/no-tags.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.json +41 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.json +364 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.proto +72 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.json +47 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/pheromon-trajectories.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.json +55 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.proto +17 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.proto +5 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.json +108 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/valid-packed.proto +35 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.proto +13 -0
- package/node_modules/protocol-buffers-schema/test/index.js +157 -0
- package/node_modules/protocol-buffers-schema/tokenize.js +56 -0
- package/node_modules/resolve-protobuf-schema/.travis.yml +3 -0
- package/node_modules/resolve-protobuf-schema/LICENSE +21 -0
- package/node_modules/resolve-protobuf-schema/README.md +49 -0
- package/node_modules/resolve-protobuf-schema/index.js +50 -0
- package/node_modules/resolve-protobuf-schema/package.json +25 -0
- package/node_modules/resolve-protobuf-schema/test/a.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/b.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/c.proto +3 -0
- package/node_modules/resolve-protobuf-schema/test/index.js +49 -0
- package/package.json +35 -11
- package/bin/stage.js +0 -52
- package/src/main/cli-args.js +0 -28
- package/src/main/context-menu.js +0 -64
- package/src/main/extensions.js +0 -61
- package/src/main/main.js +0 -310
- package/src/main/menu.js +0 -171
- package/src/main/reveal.js +0 -141
- package/src/main/selftest.js +0 -383
- package/src/main/store.js +0 -77
- package/src/main/suggest.js +0 -50
- package/src/main/tabs.js +0 -351
- package/src/main/url.js +0 -37
- package/src/main/window.js +0 -71
- package/src/preload/extension-compat.js +0 -34
- package/src/preload/ui-preload.js +0 -40
- package/src/ui/app.js +0 -588
- package/src/ui/mock.js +0 -455
- /package/{src → dist}/ui/newtab.html +0 -0
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
|
|
2
|
+
const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
|
|
3
|
+
const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
|
|
4
|
+
|
|
5
|
+
// Threshold chosen based on both benchmarking and knowledge about browser string
|
|
6
|
+
// data structures (which currently switch structure types at 12 bytes or more)
|
|
7
|
+
const TEXT_DECODER_MIN_LENGTH = 12;
|
|
8
|
+
const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
|
|
9
|
+
|
|
10
|
+
const PBF_VARINT = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
|
|
11
|
+
const PBF_FIXED64 = 1; // 64-bit: double, fixed64, sfixed64
|
|
12
|
+
const PBF_BYTES = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
|
|
13
|
+
const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32
|
|
14
|
+
|
|
15
|
+
export default class Pbf {
|
|
16
|
+
/**
|
|
17
|
+
* @param {Uint8Array | ArrayBuffer} [buf]
|
|
18
|
+
*/
|
|
19
|
+
constructor(buf = new Uint8Array(16)) {
|
|
20
|
+
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
|
|
21
|
+
this.dataView = new DataView(this.buf.buffer);
|
|
22
|
+
this.pos = 0;
|
|
23
|
+
this.type = 0;
|
|
24
|
+
this.length = this.buf.length;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// === READING =================================================================
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @template T
|
|
31
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
32
|
+
* @param {T} result
|
|
33
|
+
* @param {number} [end]
|
|
34
|
+
*/
|
|
35
|
+
readFields(readField, result, end = this.length) {
|
|
36
|
+
while (this.pos < end) {
|
|
37
|
+
const val = this.readVarint(),
|
|
38
|
+
tag = val >> 3,
|
|
39
|
+
startPos = this.pos;
|
|
40
|
+
|
|
41
|
+
this.type = val & 0x7;
|
|
42
|
+
readField(tag, result, this);
|
|
43
|
+
|
|
44
|
+
if (this.pos === startPos) this.skip(val);
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @template T
|
|
51
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
52
|
+
* @param {T} result
|
|
53
|
+
*/
|
|
54
|
+
readMessage(readField, result) {
|
|
55
|
+
return this.readFields(readField, result, this.readVarint() + this.pos);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
readFixed32() {
|
|
59
|
+
const val = this.dataView.getUint32(this.pos, true);
|
|
60
|
+
this.pos += 4;
|
|
61
|
+
return val;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
readSFixed32() {
|
|
65
|
+
const val = this.dataView.getInt32(this.pos, true);
|
|
66
|
+
this.pos += 4;
|
|
67
|
+
return val;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
|
|
71
|
+
|
|
72
|
+
readFixed64() {
|
|
73
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
74
|
+
this.pos += 8;
|
|
75
|
+
return val;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
readSFixed64() {
|
|
79
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
80
|
+
this.pos += 8;
|
|
81
|
+
return val;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
readFloat() {
|
|
85
|
+
const val = this.dataView.getFloat32(this.pos, true);
|
|
86
|
+
this.pos += 4;
|
|
87
|
+
return val;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
readDouble() {
|
|
91
|
+
const val = this.dataView.getFloat64(this.pos, true);
|
|
92
|
+
this.pos += 8;
|
|
93
|
+
return val;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {boolean} [isSigned]
|
|
98
|
+
*/
|
|
99
|
+
readVarint(isSigned) {
|
|
100
|
+
const buf = this.buf;
|
|
101
|
+
let val, b;
|
|
102
|
+
|
|
103
|
+
b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val;
|
|
104
|
+
b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val;
|
|
105
|
+
b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val;
|
|
106
|
+
b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val;
|
|
107
|
+
b = buf[this.pos]; val |= (b & 0x0f) << 28;
|
|
108
|
+
|
|
109
|
+
return readVarintRemainder(val, isSigned, this);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
readVarint64() { // for compatibility with v2.0.1
|
|
113
|
+
return this.readVarint(true);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
readSVarint() {
|
|
117
|
+
const num = this.readVarint();
|
|
118
|
+
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
readBoolean() {
|
|
122
|
+
return Boolean(this.readVarint());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
readString() {
|
|
126
|
+
const end = this.readVarint() + this.pos;
|
|
127
|
+
const pos = this.pos;
|
|
128
|
+
this.pos = end;
|
|
129
|
+
|
|
130
|
+
if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
|
|
131
|
+
// longer strings are fast with the built-in browser TextDecoder API
|
|
132
|
+
return utf8TextDecoder.decode(this.buf.subarray(pos, end));
|
|
133
|
+
}
|
|
134
|
+
// short strings are fast with our custom implementation
|
|
135
|
+
return readUtf8(this.buf, pos, end);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
readBytes() {
|
|
139
|
+
const end = this.readVarint() + this.pos,
|
|
140
|
+
buffer = this.buf.subarray(this.pos, end);
|
|
141
|
+
this.pos = end;
|
|
142
|
+
return buffer;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// verbose for performance reasons; doesn't affect gzipped size
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {number[]} [arr]
|
|
149
|
+
* @param {boolean} [isSigned]
|
|
150
|
+
*/
|
|
151
|
+
readPackedVarint(arr = [], isSigned) {
|
|
152
|
+
const end = this.readPackedEnd();
|
|
153
|
+
while (this.pos < end) arr.push(this.readVarint(isSigned));
|
|
154
|
+
return arr;
|
|
155
|
+
}
|
|
156
|
+
/** @param {number[]} [arr] */
|
|
157
|
+
readPackedSVarint(arr = []) {
|
|
158
|
+
const end = this.readPackedEnd();
|
|
159
|
+
while (this.pos < end) arr.push(this.readSVarint());
|
|
160
|
+
return arr;
|
|
161
|
+
}
|
|
162
|
+
/** @param {boolean[]} [arr] */
|
|
163
|
+
readPackedBoolean(arr = []) {
|
|
164
|
+
const end = this.readPackedEnd();
|
|
165
|
+
while (this.pos < end) arr.push(this.readBoolean());
|
|
166
|
+
return arr;
|
|
167
|
+
}
|
|
168
|
+
/** @param {number[]} [arr] */
|
|
169
|
+
readPackedFloat(arr = []) {
|
|
170
|
+
const end = this.readPackedEnd();
|
|
171
|
+
while (this.pos < end) arr.push(this.readFloat());
|
|
172
|
+
return arr;
|
|
173
|
+
}
|
|
174
|
+
/** @param {number[]} [arr] */
|
|
175
|
+
readPackedDouble(arr = []) {
|
|
176
|
+
const end = this.readPackedEnd();
|
|
177
|
+
while (this.pos < end) arr.push(this.readDouble());
|
|
178
|
+
return arr;
|
|
179
|
+
}
|
|
180
|
+
/** @param {number[]} [arr] */
|
|
181
|
+
readPackedFixed32(arr = []) {
|
|
182
|
+
const end = this.readPackedEnd();
|
|
183
|
+
while (this.pos < end) arr.push(this.readFixed32());
|
|
184
|
+
return arr;
|
|
185
|
+
}
|
|
186
|
+
/** @param {number[]} [arr] */
|
|
187
|
+
readPackedSFixed32(arr = []) {
|
|
188
|
+
const end = this.readPackedEnd();
|
|
189
|
+
while (this.pos < end) arr.push(this.readSFixed32());
|
|
190
|
+
return arr;
|
|
191
|
+
}
|
|
192
|
+
/** @param {number[]} [arr] */
|
|
193
|
+
readPackedFixed64(arr = []) {
|
|
194
|
+
const end = this.readPackedEnd();
|
|
195
|
+
while (this.pos < end) arr.push(this.readFixed64());
|
|
196
|
+
return arr;
|
|
197
|
+
}
|
|
198
|
+
/** @param {number[]} [arr] */
|
|
199
|
+
readPackedSFixed64(arr = []) {
|
|
200
|
+
const end = this.readPackedEnd();
|
|
201
|
+
while (this.pos < end) arr.push(this.readSFixed64());
|
|
202
|
+
return arr;
|
|
203
|
+
}
|
|
204
|
+
readPackedEnd() {
|
|
205
|
+
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** @param {number} val */
|
|
209
|
+
skip(val) {
|
|
210
|
+
const type = val & 0x7;
|
|
211
|
+
if (type === PBF_VARINT) while (this.buf[this.pos++] > 0x7f) {}
|
|
212
|
+
else if (type === PBF_BYTES) this.pos = this.readVarint() + this.pos;
|
|
213
|
+
else if (type === PBF_FIXED32) this.pos += 4;
|
|
214
|
+
else if (type === PBF_FIXED64) this.pos += 8;
|
|
215
|
+
else throw new Error(`Unimplemented type: ${type}`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// === WRITING =================================================================
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @param {number} tag
|
|
222
|
+
* @param {number} type
|
|
223
|
+
*/
|
|
224
|
+
writeTag(tag, type) {
|
|
225
|
+
this.writeVarint((tag << 3) | type);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** @param {number} min */
|
|
229
|
+
realloc(min) {
|
|
230
|
+
let length = this.length || 16;
|
|
231
|
+
|
|
232
|
+
while (length < this.pos + min) length *= 2;
|
|
233
|
+
|
|
234
|
+
if (length !== this.length) {
|
|
235
|
+
const buf = new Uint8Array(length);
|
|
236
|
+
buf.set(this.buf);
|
|
237
|
+
this.buf = buf;
|
|
238
|
+
this.dataView = new DataView(buf.buffer);
|
|
239
|
+
this.length = length;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
finish() {
|
|
244
|
+
this.length = this.pos;
|
|
245
|
+
this.pos = 0;
|
|
246
|
+
return this.buf.subarray(0, this.length);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** @param {number} val */
|
|
250
|
+
writeFixed32(val) {
|
|
251
|
+
this.realloc(4);
|
|
252
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
253
|
+
this.pos += 4;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** @param {number} val */
|
|
257
|
+
writeSFixed32(val) {
|
|
258
|
+
this.realloc(4);
|
|
259
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
260
|
+
this.pos += 4;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** @param {number} val */
|
|
264
|
+
writeFixed64(val) {
|
|
265
|
+
this.realloc(8);
|
|
266
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
267
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
268
|
+
this.pos += 8;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** @param {number} val */
|
|
272
|
+
writeSFixed64(val) {
|
|
273
|
+
this.realloc(8);
|
|
274
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
275
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
276
|
+
this.pos += 8;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** @param {number} val */
|
|
280
|
+
writeVarint(val) {
|
|
281
|
+
val = +val || 0;
|
|
282
|
+
|
|
283
|
+
if (val > 0xfffffff || val < 0) {
|
|
284
|
+
writeBigVarint(val, this);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
this.realloc(4);
|
|
289
|
+
|
|
290
|
+
this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
|
291
|
+
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
|
292
|
+
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
|
|
293
|
+
this.buf[this.pos++] = (val >>> 7) & 0x7f;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** @param {number} val */
|
|
297
|
+
writeSVarint(val) {
|
|
298
|
+
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** @param {boolean} val */
|
|
302
|
+
writeBoolean(val) {
|
|
303
|
+
this.writeVarint(+val);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** @param {string} str */
|
|
307
|
+
writeString(str) {
|
|
308
|
+
str = String(str);
|
|
309
|
+
this.realloc(str.length * 4);
|
|
310
|
+
|
|
311
|
+
this.pos++; // reserve 1 byte for short string length
|
|
312
|
+
|
|
313
|
+
const startPos = this.pos;
|
|
314
|
+
// write the string directly to the buffer and see how much was written
|
|
315
|
+
this.pos = writeUtf8(this.buf, str, this.pos);
|
|
316
|
+
const len = this.pos - startPos;
|
|
317
|
+
|
|
318
|
+
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
|
|
319
|
+
|
|
320
|
+
// finally, write the message length in the reserved place and restore the position
|
|
321
|
+
this.pos = startPos - 1;
|
|
322
|
+
this.writeVarint(len);
|
|
323
|
+
this.pos += len;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** @param {number} val */
|
|
327
|
+
writeFloat(val) {
|
|
328
|
+
this.realloc(4);
|
|
329
|
+
this.dataView.setFloat32(this.pos, val, true);
|
|
330
|
+
this.pos += 4;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** @param {number} val */
|
|
334
|
+
writeDouble(val) {
|
|
335
|
+
this.realloc(8);
|
|
336
|
+
this.dataView.setFloat64(this.pos, val, true);
|
|
337
|
+
this.pos += 8;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** @param {Uint8Array} buffer */
|
|
341
|
+
writeBytes(buffer) {
|
|
342
|
+
const len = buffer.length;
|
|
343
|
+
this.writeVarint(len);
|
|
344
|
+
this.realloc(len);
|
|
345
|
+
for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* @template T
|
|
350
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
351
|
+
* @param {T} obj
|
|
352
|
+
*/
|
|
353
|
+
writeRawMessage(fn, obj) {
|
|
354
|
+
this.pos++; // reserve 1 byte for short message length
|
|
355
|
+
|
|
356
|
+
// write the message directly to the buffer and see how much was written
|
|
357
|
+
const startPos = this.pos;
|
|
358
|
+
fn(obj, this);
|
|
359
|
+
const len = this.pos - startPos;
|
|
360
|
+
|
|
361
|
+
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
|
|
362
|
+
|
|
363
|
+
// finally, write the message length in the reserved place and restore the position
|
|
364
|
+
this.pos = startPos - 1;
|
|
365
|
+
this.writeVarint(len);
|
|
366
|
+
this.pos += len;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* @template T
|
|
371
|
+
* @param {number} tag
|
|
372
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
373
|
+
* @param {T} obj
|
|
374
|
+
*/
|
|
375
|
+
writeMessage(tag, fn, obj) {
|
|
376
|
+
this.writeTag(tag, PBF_BYTES);
|
|
377
|
+
this.writeRawMessage(fn, obj);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @param {number} tag
|
|
382
|
+
* @param {number[]} arr
|
|
383
|
+
*/
|
|
384
|
+
writePackedVarint(tag, arr) {
|
|
385
|
+
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* @param {number} tag
|
|
389
|
+
* @param {number[]} arr
|
|
390
|
+
*/
|
|
391
|
+
writePackedSVarint(tag, arr) {
|
|
392
|
+
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* @param {number} tag
|
|
396
|
+
* @param {boolean[]} arr
|
|
397
|
+
*/
|
|
398
|
+
writePackedBoolean(tag, arr) {
|
|
399
|
+
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* @param {number} tag
|
|
403
|
+
* @param {number[]} arr
|
|
404
|
+
*/
|
|
405
|
+
writePackedFloat(tag, arr) {
|
|
406
|
+
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* @param {number} tag
|
|
410
|
+
* @param {number[]} arr
|
|
411
|
+
*/
|
|
412
|
+
writePackedDouble(tag, arr) {
|
|
413
|
+
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* @param {number} tag
|
|
417
|
+
* @param {number[]} arr
|
|
418
|
+
*/
|
|
419
|
+
writePackedFixed32(tag, arr) {
|
|
420
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* @param {number} tag
|
|
424
|
+
* @param {number[]} arr
|
|
425
|
+
*/
|
|
426
|
+
writePackedSFixed32(tag, arr) {
|
|
427
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* @param {number} tag
|
|
431
|
+
* @param {number[]} arr
|
|
432
|
+
*/
|
|
433
|
+
writePackedFixed64(tag, arr) {
|
|
434
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* @param {number} tag
|
|
438
|
+
* @param {number[]} arr
|
|
439
|
+
*/
|
|
440
|
+
writePackedSFixed64(tag, arr) {
|
|
441
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @param {number} tag
|
|
446
|
+
* @param {Uint8Array} buffer
|
|
447
|
+
*/
|
|
448
|
+
writeBytesField(tag, buffer) {
|
|
449
|
+
this.writeTag(tag, PBF_BYTES);
|
|
450
|
+
this.writeBytes(buffer);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* @param {number} tag
|
|
454
|
+
* @param {number} val
|
|
455
|
+
*/
|
|
456
|
+
writeFixed32Field(tag, val) {
|
|
457
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
458
|
+
this.writeFixed32(val);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* @param {number} tag
|
|
462
|
+
* @param {number} val
|
|
463
|
+
*/
|
|
464
|
+
writeSFixed32Field(tag, val) {
|
|
465
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
466
|
+
this.writeSFixed32(val);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* @param {number} tag
|
|
470
|
+
* @param {number} val
|
|
471
|
+
*/
|
|
472
|
+
writeFixed64Field(tag, val) {
|
|
473
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
474
|
+
this.writeFixed64(val);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* @param {number} tag
|
|
478
|
+
* @param {number} val
|
|
479
|
+
*/
|
|
480
|
+
writeSFixed64Field(tag, val) {
|
|
481
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
482
|
+
this.writeSFixed64(val);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* @param {number} tag
|
|
486
|
+
* @param {number} val
|
|
487
|
+
*/
|
|
488
|
+
writeVarintField(tag, val) {
|
|
489
|
+
this.writeTag(tag, PBF_VARINT);
|
|
490
|
+
this.writeVarint(val);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* @param {number} tag
|
|
494
|
+
* @param {number} val
|
|
495
|
+
*/
|
|
496
|
+
writeSVarintField(tag, val) {
|
|
497
|
+
this.writeTag(tag, PBF_VARINT);
|
|
498
|
+
this.writeSVarint(val);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* @param {number} tag
|
|
502
|
+
* @param {string} str
|
|
503
|
+
*/
|
|
504
|
+
writeStringField(tag, str) {
|
|
505
|
+
this.writeTag(tag, PBF_BYTES);
|
|
506
|
+
this.writeString(str);
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* @param {number} tag
|
|
510
|
+
* @param {number} val
|
|
511
|
+
*/
|
|
512
|
+
writeFloatField(tag, val) {
|
|
513
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
514
|
+
this.writeFloat(val);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* @param {number} tag
|
|
518
|
+
* @param {number} val
|
|
519
|
+
*/
|
|
520
|
+
writeDoubleField(tag, val) {
|
|
521
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
522
|
+
this.writeDouble(val);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* @param {number} tag
|
|
526
|
+
* @param {boolean} val
|
|
527
|
+
*/
|
|
528
|
+
writeBooleanField(tag, val) {
|
|
529
|
+
this.writeVarintField(tag, +val);
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* @param {number} l
|
|
535
|
+
* @param {boolean | undefined} s
|
|
536
|
+
* @param {Pbf} p
|
|
537
|
+
*/
|
|
538
|
+
function readVarintRemainder(l, s, p) {
|
|
539
|
+
const buf = p.buf;
|
|
540
|
+
let h, b;
|
|
541
|
+
|
|
542
|
+
b = buf[p.pos++]; h = (b & 0x70) >> 4; if (b < 0x80) return toNum(l, h, s);
|
|
543
|
+
b = buf[p.pos++]; h |= (b & 0x7f) << 3; if (b < 0x80) return toNum(l, h, s);
|
|
544
|
+
b = buf[p.pos++]; h |= (b & 0x7f) << 10; if (b < 0x80) return toNum(l, h, s);
|
|
545
|
+
b = buf[p.pos++]; h |= (b & 0x7f) << 17; if (b < 0x80) return toNum(l, h, s);
|
|
546
|
+
b = buf[p.pos++]; h |= (b & 0x7f) << 24; if (b < 0x80) return toNum(l, h, s);
|
|
547
|
+
b = buf[p.pos++]; h |= (b & 0x01) << 31; if (b < 0x80) return toNum(l, h, s);
|
|
548
|
+
|
|
549
|
+
throw new Error('Expected varint not more than 10 bytes');
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* @param {number} low
|
|
554
|
+
* @param {number} high
|
|
555
|
+
* @param {boolean} [isSigned]
|
|
556
|
+
*/
|
|
557
|
+
function toNum(low, high, isSigned) {
|
|
558
|
+
return isSigned ? high * 0x100000000 + (low >>> 0) : ((high >>> 0) * 0x100000000) + (low >>> 0);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* @param {number} val
|
|
563
|
+
* @param {Pbf} pbf
|
|
564
|
+
*/
|
|
565
|
+
function writeBigVarint(val, pbf) {
|
|
566
|
+
let low, high;
|
|
567
|
+
|
|
568
|
+
if (val >= 0) {
|
|
569
|
+
low = (val % 0x100000000) | 0;
|
|
570
|
+
high = (val / 0x100000000) | 0;
|
|
571
|
+
} else {
|
|
572
|
+
low = ~(-val % 0x100000000);
|
|
573
|
+
high = ~(-val / 0x100000000);
|
|
574
|
+
|
|
575
|
+
if (low ^ 0xffffffff) {
|
|
576
|
+
low = (low + 1) | 0;
|
|
577
|
+
} else {
|
|
578
|
+
low = 0;
|
|
579
|
+
high = (high + 1) | 0;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (val >= 0x10000000000000000 || val < -0x10000000000000000) {
|
|
584
|
+
throw new Error('Given varint doesn\'t fit into 10 bytes');
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
pbf.realloc(10);
|
|
588
|
+
|
|
589
|
+
writeBigVarintLow(low, high, pbf);
|
|
590
|
+
writeBigVarintHigh(high, pbf);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @param {number} high
|
|
595
|
+
* @param {number} low
|
|
596
|
+
* @param {Pbf} pbf
|
|
597
|
+
*/
|
|
598
|
+
function writeBigVarintLow(low, high, pbf) {
|
|
599
|
+
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
|
600
|
+
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
|
601
|
+
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
|
602
|
+
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
|
|
603
|
+
pbf.buf[pbf.pos] = low & 0x7f;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* @param {number} high
|
|
608
|
+
* @param {Pbf} pbf
|
|
609
|
+
*/
|
|
610
|
+
function writeBigVarintHigh(high, pbf) {
|
|
611
|
+
const lsb = (high & 0x07) << 4;
|
|
612
|
+
|
|
613
|
+
pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0); if (!high) return;
|
|
614
|
+
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
|
615
|
+
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
|
616
|
+
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
|
617
|
+
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
|
|
618
|
+
pbf.buf[pbf.pos++] = high & 0x7f;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* @param {number} startPos
|
|
623
|
+
* @param {number} len
|
|
624
|
+
* @param {Pbf} pbf
|
|
625
|
+
*/
|
|
626
|
+
function makeRoomForExtraLength(startPos, len, pbf) {
|
|
627
|
+
const extraLen =
|
|
628
|
+
len <= 0x3fff ? 1 :
|
|
629
|
+
len <= 0x1fffff ? 2 :
|
|
630
|
+
len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
|
|
631
|
+
|
|
632
|
+
// if 1 byte isn't enough for encoding message length, shift the data to the right
|
|
633
|
+
pbf.realloc(extraLen);
|
|
634
|
+
for (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* @param {number[]} arr
|
|
639
|
+
* @param {Pbf} pbf
|
|
640
|
+
*/
|
|
641
|
+
function writePackedVarint(arr, pbf) {
|
|
642
|
+
for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* @param {number[]} arr
|
|
646
|
+
* @param {Pbf} pbf
|
|
647
|
+
*/
|
|
648
|
+
function writePackedSVarint(arr, pbf) {
|
|
649
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* @param {number[]} arr
|
|
653
|
+
* @param {Pbf} pbf
|
|
654
|
+
*/
|
|
655
|
+
function writePackedFloat(arr, pbf) {
|
|
656
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* @param {number[]} arr
|
|
660
|
+
* @param {Pbf} pbf
|
|
661
|
+
*/
|
|
662
|
+
function writePackedDouble(arr, pbf) {
|
|
663
|
+
for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* @param {boolean[]} arr
|
|
667
|
+
* @param {Pbf} pbf
|
|
668
|
+
*/
|
|
669
|
+
function writePackedBoolean(arr, pbf) {
|
|
670
|
+
for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* @param {number[]} arr
|
|
674
|
+
* @param {Pbf} pbf
|
|
675
|
+
*/
|
|
676
|
+
function writePackedFixed32(arr, pbf) {
|
|
677
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* @param {number[]} arr
|
|
681
|
+
* @param {Pbf} pbf
|
|
682
|
+
*/
|
|
683
|
+
function writePackedSFixed32(arr, pbf) {
|
|
684
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* @param {number[]} arr
|
|
688
|
+
* @param {Pbf} pbf
|
|
689
|
+
*/
|
|
690
|
+
function writePackedFixed64(arr, pbf) {
|
|
691
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* @param {number[]} arr
|
|
695
|
+
* @param {Pbf} pbf
|
|
696
|
+
*/
|
|
697
|
+
function writePackedSFixed64(arr, pbf) {
|
|
698
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// Buffer code below from https://github.com/feross/buffer, MIT-licensed
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* @param {Uint8Array} buf
|
|
705
|
+
* @param {number} pos
|
|
706
|
+
* @param {number} end
|
|
707
|
+
*/
|
|
708
|
+
function readUtf8(buf, pos, end) {
|
|
709
|
+
let str = '';
|
|
710
|
+
let i = pos;
|
|
711
|
+
|
|
712
|
+
while (i < end) {
|
|
713
|
+
const b0 = buf[i];
|
|
714
|
+
let c = null; // codepoint
|
|
715
|
+
let bytesPerSequence =
|
|
716
|
+
b0 > 0xEF ? 4 :
|
|
717
|
+
b0 > 0xDF ? 3 :
|
|
718
|
+
b0 > 0xBF ? 2 : 1;
|
|
719
|
+
|
|
720
|
+
if (i + bytesPerSequence > end) break;
|
|
721
|
+
|
|
722
|
+
let b1, b2, b3;
|
|
723
|
+
|
|
724
|
+
if (bytesPerSequence === 1) {
|
|
725
|
+
if (b0 < 0x80) {
|
|
726
|
+
c = b0;
|
|
727
|
+
}
|
|
728
|
+
} else if (bytesPerSequence === 2) {
|
|
729
|
+
b1 = buf[i + 1];
|
|
730
|
+
if ((b1 & 0xC0) === 0x80) {
|
|
731
|
+
c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
|
|
732
|
+
if (c <= 0x7F) {
|
|
733
|
+
c = null;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
} else if (bytesPerSequence === 3) {
|
|
737
|
+
b1 = buf[i + 1];
|
|
738
|
+
b2 = buf[i + 2];
|
|
739
|
+
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
|
|
740
|
+
c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
|
|
741
|
+
if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
|
|
742
|
+
c = null;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
} else if (bytesPerSequence === 4) {
|
|
746
|
+
b1 = buf[i + 1];
|
|
747
|
+
b2 = buf[i + 2];
|
|
748
|
+
b3 = buf[i + 3];
|
|
749
|
+
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
|
|
750
|
+
c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
|
|
751
|
+
if (c <= 0xFFFF || c >= 0x110000) {
|
|
752
|
+
c = null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if (c === null) {
|
|
758
|
+
c = 0xFFFD;
|
|
759
|
+
bytesPerSequence = 1;
|
|
760
|
+
|
|
761
|
+
} else if (c > 0xFFFF) {
|
|
762
|
+
c -= 0x10000;
|
|
763
|
+
str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
|
|
764
|
+
c = 0xDC00 | c & 0x3FF;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
str += String.fromCharCode(c);
|
|
768
|
+
i += bytesPerSequence;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return str;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* @param {Uint8Array} buf
|
|
776
|
+
* @param {string} str
|
|
777
|
+
* @param {number} pos
|
|
778
|
+
*/
|
|
779
|
+
function writeUtf8(buf, str, pos) {
|
|
780
|
+
for (let i = 0, c, lead; i < str.length; i++) {
|
|
781
|
+
c = str.charCodeAt(i); // code point
|
|
782
|
+
|
|
783
|
+
if (c > 0xD7FF && c < 0xE000) {
|
|
784
|
+
if (lead) {
|
|
785
|
+
if (c < 0xDC00) {
|
|
786
|
+
buf[pos++] = 0xEF;
|
|
787
|
+
buf[pos++] = 0xBF;
|
|
788
|
+
buf[pos++] = 0xBD;
|
|
789
|
+
lead = c;
|
|
790
|
+
continue;
|
|
791
|
+
} else {
|
|
792
|
+
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
|
|
793
|
+
lead = null;
|
|
794
|
+
}
|
|
795
|
+
} else {
|
|
796
|
+
if (c > 0xDBFF || (i + 1 === str.length)) {
|
|
797
|
+
buf[pos++] = 0xEF;
|
|
798
|
+
buf[pos++] = 0xBF;
|
|
799
|
+
buf[pos++] = 0xBD;
|
|
800
|
+
} else {
|
|
801
|
+
lead = c;
|
|
802
|
+
}
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
} else if (lead) {
|
|
806
|
+
buf[pos++] = 0xEF;
|
|
807
|
+
buf[pos++] = 0xBF;
|
|
808
|
+
buf[pos++] = 0xBD;
|
|
809
|
+
lead = null;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (c < 0x80) {
|
|
813
|
+
buf[pos++] = c;
|
|
814
|
+
} else {
|
|
815
|
+
if (c < 0x800) {
|
|
816
|
+
buf[pos++] = c >> 0x6 | 0xC0;
|
|
817
|
+
} else {
|
|
818
|
+
if (c < 0x10000) {
|
|
819
|
+
buf[pos++] = c >> 0xC | 0xE0;
|
|
820
|
+
} else {
|
|
821
|
+
buf[pos++] = c >> 0x12 | 0xF0;
|
|
822
|
+
buf[pos++] = c >> 0xC & 0x3F | 0x80;
|
|
823
|
+
}
|
|
824
|
+
buf[pos++] = c >> 0x6 & 0x3F | 0x80;
|
|
825
|
+
}
|
|
826
|
+
buf[pos++] = c & 0x3F | 0x80;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return pos;
|
|
830
|
+
}
|