@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.2-alpha.2
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/cjs/binary-encoding.js +414 -409
- package/dist/cjs/binary-format.js +13 -0
- package/dist/cjs/descriptor-registry.js +420 -449
- package/dist/cjs/descriptor-set.js +429 -513
- package/dist/cjs/enum.js +13 -0
- package/dist/cjs/field-list.js +13 -0
- package/dist/cjs/field.js +43 -30
- package/dist/cjs/google/protobuf/any_pb.js +122 -140
- package/dist/cjs/google/protobuf/api_pb.js +177 -224
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
- package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
- package/dist/cjs/google/protobuf/duration_pb.js +72 -88
- package/dist/cjs/google/protobuf/empty_pb.js +17 -17
- package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
- package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
- package/dist/cjs/google/protobuf/struct_pb.js +144 -181
- package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
- package/dist/cjs/google/protobuf/type_pb.js +406 -462
- package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
- package/dist/cjs/google/varint.js +166 -171
- package/dist/cjs/index.js +41 -122
- package/dist/cjs/json-format.js +13 -0
- package/dist/cjs/message-type.js +13 -0
- package/dist/cjs/message.js +89 -85
- package/dist/cjs/private/assert.js +33 -26
- package/dist/cjs/private/base64.js +93 -79
- package/dist/cjs/private/binary-format-common.js +208 -208
- package/dist/cjs/private/binary-format-proto2.js +88 -109
- package/dist/cjs/private/binary-format-proto3.js +70 -90
- package/dist/cjs/private/enum.js +77 -44
- package/dist/cjs/private/field-list.js +64 -50
- package/dist/cjs/private/field-wrapper.js +21 -10
- package/dist/cjs/private/field.js +35 -25
- package/dist/cjs/private/json-format-common.js +427 -440
- package/dist/cjs/private/json-format-proto2.js +91 -89
- package/dist/cjs/private/json-format-proto3.js +82 -93
- package/dist/cjs/private/message-type.js +39 -27
- package/dist/cjs/private/names.js +71 -62
- package/dist/cjs/private/options-map.js +13 -0
- package/dist/cjs/private/proto-runtime.js +25 -16
- package/dist/cjs/private/scalars.js +113 -103
- package/dist/cjs/private/util-common.js +193 -210
- package/dist/cjs/private/util.js +13 -0
- package/dist/cjs/proto-int64.js +123 -111
- package/dist/cjs/proto2.js +63 -63
- package/dist/cjs/proto3.js +74 -74
- package/dist/cjs/service-type.js +27 -17
- package/dist/cjs/type-registry.js +46 -31
- package/dist/esm/binary-encoding.js +414 -414
- package/dist/esm/binary-format.js +13 -0
- package/dist/esm/descriptor-registry.js +422 -458
- package/dist/esm/descriptor-set.js +430 -478
- package/dist/esm/enum.js +13 -0
- package/dist/esm/field-list.js +13 -0
- package/dist/esm/field.js +42 -29
- package/dist/esm/google/protobuf/any_pb.js +122 -140
- package/dist/esm/google/protobuf/api_pb.js +177 -197
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
- package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
- package/dist/esm/google/protobuf/duration_pb.js +72 -88
- package/dist/esm/google/protobuf/empty_pb.js +17 -17
- package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
- package/dist/esm/google/protobuf/source_context_pb.js +25 -25
- package/dist/esm/google/protobuf/struct_pb.js +143 -180
- package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
- package/dist/esm/google/protobuf/type_pb.js +401 -421
- package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
- package/dist/esm/google/varint.js +165 -164
- package/dist/esm/index.js +14 -1
- package/dist/esm/json-format.js +13 -0
- package/dist/esm/message-type.js +13 -0
- package/dist/esm/message.js +89 -85
- package/dist/esm/private/assert.js +32 -21
- package/dist/esm/private/base64.js +92 -78
- package/dist/esm/private/binary-format-common.js +209 -207
- package/dist/esm/private/binary-format-proto2.js +89 -81
- package/dist/esm/private/binary-format-proto3.js +71 -67
- package/dist/esm/private/enum.js +75 -43
- package/dist/esm/private/field-list.js +64 -50
- package/dist/esm/private/field-wrapper.js +21 -10
- package/dist/esm/private/field.js +35 -22
- package/dist/esm/private/json-format-common.js +427 -435
- package/dist/esm/private/json-format-proto2.js +89 -81
- package/dist/esm/private/json-format-proto3.js +79 -80
- package/dist/esm/private/message-type.js +40 -28
- package/dist/esm/private/names.js +70 -57
- package/dist/esm/private/options-map.js +13 -0
- package/dist/esm/private/proto-runtime.js +26 -12
- package/dist/esm/private/scalars.js +112 -99
- package/dist/esm/private/util-common.js +193 -192
- package/dist/esm/private/util.js +13 -0
- package/dist/esm/proto-int64.js +123 -111
- package/dist/esm/proto2.js +63 -57
- package/dist/esm/proto3.js +74 -68
- package/dist/esm/service-type.js +25 -12
- package/dist/esm/type-registry.js +46 -31
- package/dist/types/binary-encoding.d.ts +398 -398
- package/dist/types/binary-format.d.ts +69 -91
- package/dist/types/descriptor-registry.d.ts +23 -29
- package/dist/types/descriptor-set.d.ts +107 -128
- package/dist/types/enum.d.ts +21 -21
- package/dist/types/field-list.d.ts +21 -21
- package/dist/types/field.d.ts +191 -231
- package/dist/types/google/protobuf/any_pb.d.ts +56 -77
- package/dist/types/google/protobuf/api_pb.d.ts +144 -187
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
- package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
- package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
- package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
- package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
- package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
- package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
- package/dist/types/google/protobuf/type_pb.d.ts +355 -422
- package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
- package/dist/types/google/varint.d.ts +6 -13
- package/dist/types/index.d.ts +4 -27
- package/dist/types/json-format.d.ts +70 -89
- package/dist/types/message-type.d.ts +37 -45
- package/dist/types/message.d.ts +62 -87
- package/dist/types/private/assert.d.ts +1 -4
- package/dist/types/private/binary-format-common.d.ts +6 -32
- package/dist/types/private/enum.d.ts +13 -11
- package/dist/types/private/field-list.d.ts +14 -21
- package/dist/types/private/field-wrapper.d.ts +4 -10
- package/dist/types/private/field.d.ts +12 -12
- package/dist/types/private/json-format-common.d.ts +5 -27
- package/dist/types/private/message-type.d.ts +12 -7
- package/dist/types/private/names.d.ts +1 -4
- package/dist/types/private/options-map.d.ts +1 -1
- package/dist/types/private/proto-runtime.d.ts +34 -42
- package/dist/types/private/scalars.d.ts +5 -12
- package/dist/types/private/util-common.d.ts +1 -4
- package/dist/types/private/util.d.ts +29 -41
- package/dist/types/proto-int64.d.ts +42 -42
- package/dist/types/service-type.d.ts +36 -56
- package/dist/types/type-registry.d.ts +13 -15
- package/package.json +2 -3
package/dist/cjs/message.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
2
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
16
|
exports.Message = void 0;
|
|
4
17
|
/**
|
|
@@ -9,90 +22,81 @@ exports.Message = void 0;
|
|
|
9
22
|
* run time, use proto3.makeMessageType().
|
|
10
23
|
*/
|
|
11
24
|
class Message {
|
|
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
|
-
* the protobuf message declaration and provides metadata for reflection-
|
|
89
|
-
* based operations.
|
|
90
|
-
*/
|
|
91
|
-
getType() {
|
|
92
|
-
// Any class that extends Message _must_ provide a complete static
|
|
93
|
-
// implementation of MessageType.
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
|
|
95
|
-
return Object.getPrototypeOf(this).constructor;
|
|
96
|
-
}
|
|
25
|
+
/**
|
|
26
|
+
* Compare with a message of the same type.
|
|
27
|
+
*/
|
|
28
|
+
equals(other) {
|
|
29
|
+
return this.getType().runtime.util.equals(this.getType(), this, other);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a deep copy.
|
|
33
|
+
*/
|
|
34
|
+
clone() {
|
|
35
|
+
// return this.getType().runtime.util.clone(this);
|
|
36
|
+
return this.getType().runtime.util.clone(this);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Parse from binary data, merging fields.
|
|
40
|
+
*
|
|
41
|
+
* Repeated fields are appended. Map entries are added, overwriting
|
|
42
|
+
* existing keys.
|
|
43
|
+
*
|
|
44
|
+
* If a message field is already present, it will be merged with the
|
|
45
|
+
* new data.
|
|
46
|
+
*/
|
|
47
|
+
fromBinary(bytes, options) {
|
|
48
|
+
const type = this.getType(), format = type.runtime.bin, opt = format.makeReadOptions(options);
|
|
49
|
+
format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Parse a message from a JSON value.
|
|
54
|
+
*/
|
|
55
|
+
fromJson(jsonValue, options) {
|
|
56
|
+
const type = this.getType(), format = type.runtime.json, opt = format.makeReadOptions(options);
|
|
57
|
+
format.readMessage(type, jsonValue, opt, this);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Parse a message from a JSON string.
|
|
62
|
+
*/
|
|
63
|
+
fromJsonString(jsonString, options) {
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
|
|
65
|
+
return this.fromJson(JSON.parse(jsonString), options);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Serialize the message to binary data.
|
|
69
|
+
*/
|
|
70
|
+
toBinary(options) {
|
|
71
|
+
const type = this.getType(), bin = type.runtime.bin, opt = bin.makeWriteOptions(options), writer = opt.writerFactory();
|
|
72
|
+
bin.writeMessage(this, writer, opt);
|
|
73
|
+
return writer.finish();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Serialize the message to a JSON value, a JavaScript value that can be
|
|
77
|
+
* passed to JSON.stringify().
|
|
78
|
+
*/
|
|
79
|
+
toJson(options) {
|
|
80
|
+
const type = this.getType(), json = type.runtime.json, opt = json.makeWriteOptions(options);
|
|
81
|
+
return json.writeMessage(this, opt);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Serialize the message to a JSON string.
|
|
85
|
+
*/
|
|
86
|
+
toJsonString(options) {
|
|
87
|
+
const value = this.toJson(options);
|
|
88
|
+
return JSON.stringify(value, null, options?.prettySpaces ?? 0);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Retrieve the MessageType of this message - a singleton that represents
|
|
92
|
+
* the protobuf message declaration and provides metadata for reflection-
|
|
93
|
+
* based operations.
|
|
94
|
+
*/
|
|
95
|
+
getType() {
|
|
96
|
+
// Any class that extends Message _must_ provide a complete static
|
|
97
|
+
// implementation of MessageType.
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
|
|
99
|
+
return Object.getPrototypeOf(this).constructor;
|
|
100
|
+
}
|
|
97
101
|
}
|
|
98
102
|
exports.Message = Message;
|
|
@@ -1,52 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
2
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertFloat32 =
|
|
4
|
-
exports.assertUInt32 =
|
|
5
|
-
exports.assertInt32 =
|
|
6
|
-
exports.assert =
|
|
7
|
-
void 0;
|
|
16
|
+
exports.assertFloat32 = exports.assertUInt32 = exports.assertInt32 = exports.assert = void 0;
|
|
8
17
|
/**
|
|
9
18
|
* Assert that condition is truthy or throw error (with message)
|
|
10
19
|
*/
|
|
11
20
|
function assert(condition, msg) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
|
|
22
|
+
if (!condition) {
|
|
23
|
+
throw new Error(msg);
|
|
24
|
+
}
|
|
16
25
|
}
|
|
17
26
|
exports.assert = assert;
|
|
18
|
-
const FLOAT32_MAX = 3.4028234663852886e38,
|
|
19
|
-
FLOAT32_MIN = -3.4028234663852886e38,
|
|
20
|
-
UINT32_MAX = 0xffffffff,
|
|
21
|
-
INT32_MAX = 0x7fffffff,
|
|
22
|
-
INT32_MIN = -0x80000000;
|
|
27
|
+
const FLOAT32_MAX = 3.4028234663852886e38, FLOAT32_MIN = -3.4028234663852886e38, UINT32_MAX = 0xffffffff, INT32_MAX = 0x7fffffff, INT32_MIN = -0x80000000;
|
|
23
28
|
/**
|
|
24
29
|
* Assert a valid signed protobuf 32-bit integer.
|
|
25
30
|
*/
|
|
26
31
|
function assertInt32(arg) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
if (typeof arg !== "number")
|
|
33
|
+
throw new Error("invalid int 32: " + typeof arg);
|
|
34
|
+
if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
|
|
35
|
+
throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
30
36
|
}
|
|
31
37
|
exports.assertInt32 = assertInt32;
|
|
32
38
|
/**
|
|
33
39
|
* Assert a valid unsigned protobuf 32-bit integer.
|
|
34
40
|
*/
|
|
35
41
|
function assertUInt32(arg) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
if (typeof arg !== "number")
|
|
43
|
+
throw new Error("invalid uint 32: " + typeof arg);
|
|
44
|
+
if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
|
|
45
|
+
throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
40
46
|
}
|
|
41
47
|
exports.assertUInt32 = assertUInt32;
|
|
42
48
|
/**
|
|
43
49
|
* Assert a valid protobuf float value.
|
|
44
50
|
*/
|
|
45
51
|
function assertFloat32(arg) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
if (typeof arg !== "number")
|
|
53
|
+
throw new Error("invalid float 32: " + typeof arg);
|
|
54
|
+
if (!Number.isFinite(arg))
|
|
55
|
+
return;
|
|
56
|
+
if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
|
|
57
|
+
throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
51
58
|
}
|
|
52
59
|
exports.assertFloat32 = assertFloat32;
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
3
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
16
|
exports.base64encode = exports.base64decode = void 0;
|
|
17
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
|
|
5
18
|
// lookup table from base64 character to byte
|
|
6
|
-
let encTable =
|
|
7
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
19
|
+
let encTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
8
20
|
// lookup table from base64 character *code* to byte because lookup by number is fast
|
|
9
21
|
let decTable = [];
|
|
10
22
|
for (let i = 0; i < encTable.length; i++)
|
|
11
|
-
|
|
23
|
+
decTable[encTable[i].charCodeAt(0)] = i;
|
|
12
24
|
// support base64url variants
|
|
13
25
|
decTable["-".charCodeAt(0)] = encTable.indexOf("+");
|
|
14
26
|
decTable["_".charCodeAt(0)] = encTable.indexOf("/");
|
|
@@ -24,57 +36,59 @@ decTable["_".charCodeAt(0)] = encTable.indexOf("/");
|
|
|
24
36
|
* no padding
|
|
25
37
|
*/
|
|
26
38
|
function base64decode(base64Str) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
// estimate byte size, not accounting for inner padding and whitespace
|
|
40
|
+
let es = (base64Str.length * 3) / 4;
|
|
41
|
+
// if (es % 3 !== 0)
|
|
42
|
+
// throw new Error("invalid base64 string");
|
|
43
|
+
if (base64Str[base64Str.length - 2] == "=")
|
|
44
|
+
es -= 2;
|
|
45
|
+
else if (base64Str[base64Str.length - 1] == "=")
|
|
46
|
+
es -= 1;
|
|
47
|
+
let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
|
|
35
48
|
groupPos = 0, // position in base64 group
|
|
36
49
|
b, // current byte
|
|
37
50
|
p = 0; // previous byte
|
|
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
|
-
|
|
51
|
+
for (let i = 0; i < base64Str.length; i++) {
|
|
52
|
+
b = decTable[base64Str.charCodeAt(i)];
|
|
53
|
+
if (b === undefined) {
|
|
54
|
+
switch (base64Str[i]) {
|
|
55
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
56
|
+
case "=":
|
|
57
|
+
groupPos = 0; // reset state when padding found
|
|
58
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
59
|
+
case "\n":
|
|
60
|
+
case "\r":
|
|
61
|
+
case "\t":
|
|
62
|
+
case " ":
|
|
63
|
+
continue; // skip white-space, and padding
|
|
64
|
+
default:
|
|
65
|
+
throw Error("invalid base64 string.");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
switch (groupPos) {
|
|
69
|
+
case 0:
|
|
70
|
+
p = b;
|
|
71
|
+
groupPos = 1;
|
|
72
|
+
break;
|
|
73
|
+
case 1:
|
|
74
|
+
bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
|
|
75
|
+
p = b;
|
|
76
|
+
groupPos = 2;
|
|
77
|
+
break;
|
|
78
|
+
case 2:
|
|
79
|
+
bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
|
|
80
|
+
p = b;
|
|
81
|
+
groupPos = 3;
|
|
82
|
+
break;
|
|
83
|
+
case 3:
|
|
84
|
+
bytes[bytePos++] = ((p & 3) << 6) | b;
|
|
85
|
+
groupPos = 0;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
74
88
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
89
|
+
if (groupPos == 1)
|
|
90
|
+
throw Error("invalid base64 string.");
|
|
91
|
+
return bytes.subarray(0, bytePos);
|
|
78
92
|
}
|
|
79
93
|
exports.base64decode = base64decode;
|
|
80
94
|
/**
|
|
@@ -83,36 +97,36 @@ exports.base64decode = base64decode;
|
|
|
83
97
|
* Does not insert newlines.
|
|
84
98
|
*/
|
|
85
99
|
function base64encode(bytes) {
|
|
86
|
-
|
|
87
|
-
groupPos = 0, // position in base64 group
|
|
100
|
+
let base64 = "", groupPos = 0, // position in base64 group
|
|
88
101
|
b, // current byte
|
|
89
102
|
p = 0; // carry over from previous byte
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
104
|
+
b = bytes[i];
|
|
105
|
+
switch (groupPos) {
|
|
106
|
+
case 0:
|
|
107
|
+
base64 += encTable[b >> 2];
|
|
108
|
+
p = (b & 3) << 4;
|
|
109
|
+
groupPos = 1;
|
|
110
|
+
break;
|
|
111
|
+
case 1:
|
|
112
|
+
base64 += encTable[p | (b >> 4)];
|
|
113
|
+
p = (b & 15) << 2;
|
|
114
|
+
groupPos = 2;
|
|
115
|
+
break;
|
|
116
|
+
case 2:
|
|
117
|
+
base64 += encTable[p | (b >> 6)];
|
|
118
|
+
base64 += encTable[b & 63];
|
|
119
|
+
groupPos = 0;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// padding required?
|
|
124
|
+
if (groupPos) {
|
|
125
|
+
base64 += encTable[p];
|
|
126
|
+
base64 += "=";
|
|
127
|
+
if (groupPos == 1)
|
|
128
|
+
base64 += "=";
|
|
108
129
|
}
|
|
109
|
-
|
|
110
|
-
// padding required?
|
|
111
|
-
if (groupPos) {
|
|
112
|
-
base64 += encTable[p];
|
|
113
|
-
base64 += "=";
|
|
114
|
-
if (groupPos == 1) base64 += "=";
|
|
115
|
-
}
|
|
116
|
-
return base64;
|
|
130
|
+
return base64;
|
|
117
131
|
}
|
|
118
132
|
exports.base64encode = base64encode;
|