@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.1
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 +21 -1
- package/dist/cjs/binary-encoding.js +402 -410
- package/dist/cjs/descriptor-registry.js +407 -449
- package/dist/cjs/descriptor-set.js +416 -513
- package/dist/cjs/field.js +30 -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/index.js +24 -122
- package/dist/cjs/message.js +81 -89
- package/dist/cjs/private/assert.js +20 -26
- package/dist/cjs/private/base64.js +79 -78
- package/dist/cjs/private/binary-format-common.js +195 -208
- package/dist/cjs/private/binary-format-proto2.js +75 -109
- package/dist/cjs/private/binary-format-proto3.js +57 -90
- package/dist/cjs/private/enum.js +38 -43
- package/dist/cjs/private/field-list.js +51 -50
- package/dist/cjs/private/field-wrapper.js +8 -10
- package/dist/cjs/private/field.js +22 -25
- package/dist/cjs/private/goog-varint.js +275 -0
- package/dist/cjs/private/json-format-common.js +414 -440
- package/dist/cjs/private/json-format-proto2.js +78 -89
- package/dist/cjs/private/json-format-proto3.js +69 -93
- package/dist/cjs/private/message-type.js +26 -27
- package/dist/cjs/private/names.js +58 -62
- package/dist/cjs/private/proto-runtime.js +11 -16
- package/dist/cjs/private/scalars.js +100 -103
- package/dist/cjs/private/util-common.js +180 -210
- package/dist/cjs/proto-int64.js +111 -112
- package/dist/cjs/proto2.js +50 -63
- package/dist/cjs/proto3.js +61 -74
- package/dist/cjs/service-type.js +14 -17
- package/dist/cjs/type-registry.js +33 -31
- package/dist/esm/binary-encoding.js +401 -414
- package/dist/esm/descriptor-registry.js +409 -458
- package/dist/esm/descriptor-set.js +417 -478
- package/dist/esm/field.js +29 -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/index.js +1 -1
- package/dist/esm/message.js +81 -89
- package/dist/esm/private/assert.js +19 -21
- package/dist/esm/private/base64.js +79 -78
- package/dist/esm/private/binary-format-common.js +196 -207
- package/dist/esm/private/binary-format-proto2.js +76 -81
- package/dist/esm/private/binary-format-proto3.js +58 -67
- package/dist/esm/private/enum.js +38 -43
- package/dist/esm/private/field-list.js +51 -50
- package/dist/esm/private/field-wrapper.js +8 -10
- package/dist/esm/private/field.js +22 -22
- package/dist/esm/private/goog-varint.js +266 -0
- package/dist/esm/private/json-format-common.js +414 -435
- package/dist/esm/private/json-format-proto2.js +76 -81
- package/dist/esm/private/json-format-proto3.js +66 -80
- package/dist/esm/private/message-type.js +27 -28
- package/dist/esm/private/names.js +57 -57
- package/dist/esm/private/proto-runtime.js +11 -11
- package/dist/esm/private/scalars.js +99 -99
- package/dist/esm/private/util-common.js +180 -192
- package/dist/esm/proto-int64.js +111 -112
- package/dist/esm/proto2.js +50 -57
- package/dist/esm/proto3.js +61 -68
- package/dist/esm/service-type.js +12 -12
- package/dist/esm/type-registry.js +33 -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/index.d.ts +4 -27
- package/dist/types/json-format.d.ts +70 -89
- package/dist/types/message-type.d.ts +38 -46
- package/dist/types/message.d.ts +73 -99
- 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 +4 -11
- package/dist/types/private/field-list.d.ts +14 -21
- package/dist/types/private/field-wrapper.d.ts +5 -11
- package/dist/types/private/field.d.ts +12 -12
- package/dist/types/{google/varint.d.ts → private/goog-varint.d.ts} +6 -13
- package/dist/types/private/json-format-common.d.ts +5 -27
- package/dist/types/private/message-type.d.ts +3 -8
- 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 +29 -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 +37 -57
- package/dist/types/type-registry.d.ts +13 -15
- package/package.json +2 -2
- package/dist/cjs/google/varint.js +0 -280
- package/dist/esm/google/varint.js +0 -265
package/dist/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { proto3 } from "./proto3.js";
|
|
2
2
|
export { proto2 } from "./proto2.js";
|
|
3
3
|
export { protoInt64 } from "./proto-int64.js";
|
|
4
|
-
export { Message } from "./message.js";
|
|
4
|
+
export { Message, } from "./message.js";
|
|
5
5
|
export { ScalarType } from "./field.js";
|
|
6
6
|
export { MethodKind, MethodIdempotency } from "./service-type.js";
|
|
7
7
|
export { TypeRegistry } from "./type-registry.js";
|
package/dist/esm/message.js
CHANGED
|
@@ -1,94 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Every message - generated or created at runtime - is an instance of this
|
|
3
|
+
* class.
|
|
4
4
|
*
|
|
5
|
-
* It is
|
|
6
|
-
*
|
|
5
|
+
* It is unsafe to extend this class. If you want to create a message at run
|
|
6
|
+
* time, use proto3.makeMessageType().
|
|
7
7
|
*/
|
|
8
8
|
export class Message {
|
|
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
|
-
* based operations.
|
|
87
|
-
*/
|
|
88
|
-
getType() {
|
|
89
|
-
// Any class that extends Message _must_ provide a complete static
|
|
90
|
-
// implementation of MessageType.
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
|
|
92
|
-
return Object.getPrototypeOf(this).constructor;
|
|
93
|
-
}
|
|
9
|
+
// TODO find better name to prevent field name conflicts?
|
|
10
|
+
/**
|
|
11
|
+
* Compare with a message of the same type.
|
|
12
|
+
*/
|
|
13
|
+
equals(other) {
|
|
14
|
+
return this.getType().runtime.util.equals(this.getType(), this, other);
|
|
15
|
+
}
|
|
16
|
+
// TODO find better name to prevent field name conflicts?
|
|
17
|
+
/**
|
|
18
|
+
* Create a deep copy.
|
|
19
|
+
*/
|
|
20
|
+
clone() {
|
|
21
|
+
return this.getType().runtime.util.clone(this);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse from binary data, merging fields.
|
|
25
|
+
*
|
|
26
|
+
* Repeated fields are appended. Map entries are added, overwriting
|
|
27
|
+
* existing keys.
|
|
28
|
+
*
|
|
29
|
+
* If a message field is already present, it will be merged with the
|
|
30
|
+
* new data.
|
|
31
|
+
*/
|
|
32
|
+
fromBinary(bytes, options) {
|
|
33
|
+
const type = this.getType(), format = type.runtime.bin, opt = format.makeReadOptions(options);
|
|
34
|
+
format.readMessage(this, opt.readerFactory(bytes), bytes.byteLength, opt);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse a message from a JSON value.
|
|
39
|
+
*/
|
|
40
|
+
fromJson(jsonValue, options) {
|
|
41
|
+
const type = this.getType(), format = type.runtime.json, opt = format.makeReadOptions(options);
|
|
42
|
+
format.readMessage(type, jsonValue, opt, this);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Parse a message from a JSON string.
|
|
47
|
+
*/
|
|
48
|
+
fromJsonString(jsonString, options) {
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument -- assigning to JsonValue is safe here
|
|
50
|
+
return this.fromJson(JSON.parse(jsonString), options);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Serialize the message to binary data.
|
|
54
|
+
*/
|
|
55
|
+
toBinary(options) {
|
|
56
|
+
const type = this.getType(), bin = type.runtime.bin, opt = bin.makeWriteOptions(options), writer = opt.writerFactory();
|
|
57
|
+
bin.writeMessage(this, writer, opt);
|
|
58
|
+
return writer.finish();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Serialize the message to a JSON value, a JavaScript value that can be
|
|
62
|
+
* passed to JSON.stringify().
|
|
63
|
+
*/
|
|
64
|
+
toJson(options) {
|
|
65
|
+
const type = this.getType(), json = type.runtime.json, opt = json.makeWriteOptions(options);
|
|
66
|
+
return json.writeMessage(this, opt);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Serialize the message to a JSON string.
|
|
70
|
+
*/
|
|
71
|
+
toJsonString(options) {
|
|
72
|
+
const value = this.toJson(options);
|
|
73
|
+
return JSON.stringify(value, null, options?.prettySpaces ?? 0);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Retrieve the MessageType of this message - a singleton that represents
|
|
77
|
+
* the protobuf message declaration and provides metadata for reflection-
|
|
78
|
+
* based operations.
|
|
79
|
+
*/
|
|
80
|
+
getType() {
|
|
81
|
+
// Any class that extends Message _must_ provide a complete static
|
|
82
|
+
// implementation of MessageType.
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return
|
|
84
|
+
return Object.getPrototypeOf(this).constructor;
|
|
85
|
+
}
|
|
94
86
|
}
|
|
@@ -2,40 +2,38 @@
|
|
|
2
2
|
* Assert that condition is truthy or throw error (with message)
|
|
3
3
|
*/
|
|
4
4
|
export function assert(condition, msg) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
|
|
6
|
+
if (!condition) {
|
|
7
|
+
throw new Error(msg);
|
|
8
|
+
}
|
|
9
9
|
}
|
|
10
|
-
const FLOAT32_MAX = 3.4028234663852886e38,
|
|
11
|
-
FLOAT32_MIN = -3.4028234663852886e38,
|
|
12
|
-
UINT32_MAX = 0xffffffff,
|
|
13
|
-
INT32_MAX = 0x7fffffff,
|
|
14
|
-
INT32_MIN = -0x80000000;
|
|
10
|
+
const FLOAT32_MAX = 3.4028234663852886e38, FLOAT32_MIN = -3.4028234663852886e38, UINT32_MAX = 0xffffffff, INT32_MAX = 0x7fffffff, INT32_MIN = -0x80000000;
|
|
15
11
|
/**
|
|
16
12
|
* Assert a valid signed protobuf 32-bit integer.
|
|
17
13
|
*/
|
|
18
14
|
export function assertInt32(arg) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
if (typeof arg !== "number")
|
|
16
|
+
throw new Error("invalid int 32: " + typeof arg);
|
|
17
|
+
if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
|
|
18
|
+
throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
22
19
|
}
|
|
23
20
|
/**
|
|
24
21
|
* Assert a valid unsigned protobuf 32-bit integer.
|
|
25
22
|
*/
|
|
26
23
|
export function assertUInt32(arg) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
if (typeof arg !== "number")
|
|
25
|
+
throw new Error("invalid uint 32: " + typeof arg);
|
|
26
|
+
if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
|
|
27
|
+
throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
31
28
|
}
|
|
32
29
|
/**
|
|
33
30
|
* Assert a valid protobuf float value.
|
|
34
31
|
*/
|
|
35
32
|
export function assertFloat32(arg) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
if (typeof arg !== "number")
|
|
34
|
+
throw new Error("invalid float 32: " + typeof arg);
|
|
35
|
+
if (!Number.isFinite(arg))
|
|
36
|
+
return;
|
|
37
|
+
if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
|
|
38
|
+
throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
|
|
41
39
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
|
|
2
2
|
// lookup table from base64 character to byte
|
|
3
|
-
let encTable =
|
|
4
|
-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
3
|
+
let encTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
5
4
|
// lookup table from base64 character *code* to byte because lookup by number is fast
|
|
6
5
|
let decTable = [];
|
|
7
6
|
for (let i = 0; i < encTable.length; i++)
|
|
8
|
-
|
|
7
|
+
decTable[encTable[i].charCodeAt(0)] = i;
|
|
9
8
|
// support base64url variants
|
|
10
9
|
decTable["-".charCodeAt(0)] = encTable.indexOf("+");
|
|
11
10
|
decTable["_".charCodeAt(0)] = encTable.indexOf("/");
|
|
@@ -21,57 +20,59 @@ decTable["_".charCodeAt(0)] = encTable.indexOf("/");
|
|
|
21
20
|
* no padding
|
|
22
21
|
*/
|
|
23
22
|
export function base64decode(base64Str) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
// estimate byte size, not accounting for inner padding and whitespace
|
|
24
|
+
let es = (base64Str.length * 3) / 4;
|
|
25
|
+
// if (es % 3 !== 0)
|
|
26
|
+
// throw new Error("invalid base64 string");
|
|
27
|
+
if (base64Str[base64Str.length - 2] == "=")
|
|
28
|
+
es -= 2;
|
|
29
|
+
else if (base64Str[base64Str.length - 1] == "=")
|
|
30
|
+
es -= 1;
|
|
31
|
+
let bytes = new Uint8Array(es), bytePos = 0, // position in byte array
|
|
32
32
|
groupPos = 0, // position in base64 group
|
|
33
33
|
b, // current byte
|
|
34
34
|
p = 0; // previous byte
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
for (let i = 0; i < base64Str.length; i++) {
|
|
36
|
+
b = decTable[base64Str.charCodeAt(i)];
|
|
37
|
+
if (b === undefined) {
|
|
38
|
+
switch (base64Str[i]) {
|
|
39
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
40
|
+
case "=":
|
|
41
|
+
groupPos = 0; // reset state when padding found
|
|
42
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
43
|
+
case "\n":
|
|
44
|
+
case "\r":
|
|
45
|
+
case "\t":
|
|
46
|
+
case " ":
|
|
47
|
+
continue; // skip white-space, and padding
|
|
48
|
+
default:
|
|
49
|
+
throw Error("invalid base64 string.");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
switch (groupPos) {
|
|
53
|
+
case 0:
|
|
54
|
+
p = b;
|
|
55
|
+
groupPos = 1;
|
|
56
|
+
break;
|
|
57
|
+
case 1:
|
|
58
|
+
bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
|
|
59
|
+
p = b;
|
|
60
|
+
groupPos = 2;
|
|
61
|
+
break;
|
|
62
|
+
case 2:
|
|
63
|
+
bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
|
|
64
|
+
p = b;
|
|
65
|
+
groupPos = 3;
|
|
66
|
+
break;
|
|
67
|
+
case 3:
|
|
68
|
+
bytes[bytePos++] = ((p & 3) << 6) | b;
|
|
69
|
+
groupPos = 0;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
51
72
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
groupPos = 1;
|
|
56
|
-
break;
|
|
57
|
-
case 1:
|
|
58
|
-
bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
|
|
59
|
-
p = b;
|
|
60
|
-
groupPos = 2;
|
|
61
|
-
break;
|
|
62
|
-
case 2:
|
|
63
|
-
bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
|
|
64
|
-
p = b;
|
|
65
|
-
groupPos = 3;
|
|
66
|
-
break;
|
|
67
|
-
case 3:
|
|
68
|
-
bytes[bytePos++] = ((p & 3) << 6) | b;
|
|
69
|
-
groupPos = 0;
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (groupPos == 1) throw Error("invalid base64 string.");
|
|
74
|
-
return bytes.subarray(0, bytePos);
|
|
73
|
+
if (groupPos == 1)
|
|
74
|
+
throw Error("invalid base64 string.");
|
|
75
|
+
return bytes.subarray(0, bytePos);
|
|
75
76
|
}
|
|
76
77
|
/**
|
|
77
78
|
* Encodes a byte array to a base64 string.
|
|
@@ -79,35 +80,35 @@ export function base64decode(base64Str) {
|
|
|
79
80
|
* Does not insert newlines.
|
|
80
81
|
*/
|
|
81
82
|
export function base64encode(bytes) {
|
|
82
|
-
|
|
83
|
-
groupPos = 0, // position in base64 group
|
|
83
|
+
let base64 = "", groupPos = 0, // position in base64 group
|
|
84
84
|
b, // current byte
|
|
85
85
|
p = 0; // carry over from previous byte
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
86
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
87
|
+
b = bytes[i];
|
|
88
|
+
switch (groupPos) {
|
|
89
|
+
case 0:
|
|
90
|
+
base64 += encTable[b >> 2];
|
|
91
|
+
p = (b & 3) << 4;
|
|
92
|
+
groupPos = 1;
|
|
93
|
+
break;
|
|
94
|
+
case 1:
|
|
95
|
+
base64 += encTable[p | (b >> 4)];
|
|
96
|
+
p = (b & 15) << 2;
|
|
97
|
+
groupPos = 2;
|
|
98
|
+
break;
|
|
99
|
+
case 2:
|
|
100
|
+
base64 += encTable[p | (b >> 6)];
|
|
101
|
+
base64 += encTable[b & 63];
|
|
102
|
+
groupPos = 0;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// padding required?
|
|
107
|
+
if (groupPos) {
|
|
108
|
+
base64 += encTable[p];
|
|
109
|
+
base64 += "=";
|
|
110
|
+
if (groupPos == 1)
|
|
111
|
+
base64 += "=";
|
|
104
112
|
}
|
|
105
|
-
|
|
106
|
-
// padding required?
|
|
107
|
-
if (groupPos) {
|
|
108
|
-
base64 += encTable[p];
|
|
109
|
-
base64 += "=";
|
|
110
|
-
if (groupPos == 1) base64 += "=";
|
|
111
|
-
}
|
|
112
|
-
return base64;
|
|
113
|
+
return base64;
|
|
113
114
|
}
|