@celljs/grpc 0.0.0-stage → 4.4.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 +121 -2
- package/cell.schema.json +32 -0
- package/cell.yml +9 -0
- package/esm/node/call/async-queue.d.ts +31 -0
- package/esm/node/call/async-queue.js +80 -0
- package/esm/node/call/async-queue.js.map +1 -0
- package/esm/node/call/grpc-status.d.ts +33 -0
- package/esm/node/call/grpc-status.js +48 -0
- package/esm/node/call/grpc-status.js.map +1 -0
- package/esm/node/call/message-framer.d.ts +24 -0
- package/esm/node/call/message-framer.js +105 -0
- package/esm/node/call/message-framer.js.map +1 -0
- package/esm/node/call/metadata.d.ts +15 -0
- package/esm/node/call/metadata.js +81 -0
- package/esm/node/call/metadata.js.map +1 -0
- package/esm/node/client/grpc-channel.d.ts +64 -0
- package/esm/node/client/grpc-channel.js +285 -0
- package/esm/node/client/grpc-channel.js.map +1 -0
- package/esm/node/codec/proto-reader.d.ts +26 -0
- package/esm/node/codec/proto-reader.js +153 -0
- package/esm/node/codec/proto-reader.js.map +1 -0
- package/esm/node/codec/proto-writer.d.ts +29 -0
- package/esm/node/codec/proto-writer.js +150 -0
- package/esm/node/codec/proto-writer.js.map +1 -0
- package/esm/node/index.d.ts +15 -0
- package/esm/node/index.js +16 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/proto/proto-ast.d.ts +46 -0
- package/esm/node/proto/proto-ast.js +2 -0
- package/esm/node/proto/proto-ast.js.map +1 -0
- package/esm/node/proto/proto-parser.d.ts +45 -0
- package/esm/node/proto/proto-parser.js +451 -0
- package/esm/node/proto/proto-parser.js.map +1 -0
- package/esm/node/proto/proto-types.d.ts +76 -0
- package/esm/node/proto/proto-types.js +444 -0
- package/esm/node/proto/proto-types.js.map +1 -0
- package/esm/node/proto/proto.d.ts +57 -0
- package/esm/node/proto/proto.js +174 -0
- package/esm/node/proto/proto.js.map +1 -0
- package/esm/node/proto/well-known.d.ts +5 -0
- package/esm/node/proto/well-known.js +41 -0
- package/esm/node/proto/well-known.js.map +1 -0
- package/esm/node/server/grpc-server-lifecycle.d.ts +21 -0
- package/esm/node/server/grpc-server-lifecycle.js +65 -0
- package/esm/node/server/grpc-server-lifecycle.js.map +1 -0
- package/esm/node/server/grpc-server.d.ts +60 -0
- package/esm/node/server/grpc-server.js +312 -0
- package/esm/node/server/grpc-server.js.map +1 -0
- package/esm/node/server/grpc-service.d.ts +24 -0
- package/esm/node/server/grpc-service.js +38 -0
- package/esm/node/server/grpc-service.js.map +1 -0
- package/esm/node/test/proto-loader.d.ts +2 -0
- package/esm/node/test/proto-loader.js +13 -0
- package/esm/node/test/proto-loader.js.map +1 -0
- package/esm/node/test/test-protos.d.ts +28 -0
- package/esm/node/test/test-protos.js +145 -0
- package/esm/node/test/test-protos.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/call/async-queue.d.ts +32 -0
- package/lib/node/call/async-queue.d.ts.map +1 -0
- package/lib/node/call/async-queue.js +84 -0
- package/lib/node/call/async-queue.js.map +1 -0
- package/lib/node/call/grpc-status.d.ts +34 -0
- package/lib/node/call/grpc-status.d.ts.map +1 -0
- package/lib/node/call/grpc-status.js +52 -0
- package/lib/node/call/grpc-status.js.map +1 -0
- package/lib/node/call/message-framer.d.ts +25 -0
- package/lib/node/call/message-framer.d.ts.map +1 -0
- package/lib/node/call/message-framer.js +109 -0
- package/lib/node/call/message-framer.js.map +1 -0
- package/lib/node/call/metadata.d.ts +16 -0
- package/lib/node/call/metadata.d.ts.map +1 -0
- package/lib/node/call/metadata.js +84 -0
- package/lib/node/call/metadata.js.map +1 -0
- package/lib/node/client/grpc-channel.d.ts +65 -0
- package/lib/node/client/grpc-channel.d.ts.map +1 -0
- package/lib/node/client/grpc-channel.js +289 -0
- package/lib/node/client/grpc-channel.js.map +1 -0
- package/lib/node/codec/proto-reader.d.ts +27 -0
- package/lib/node/codec/proto-reader.d.ts.map +1 -0
- package/lib/node/codec/proto-reader.js +157 -0
- package/lib/node/codec/proto-reader.js.map +1 -0
- package/lib/node/codec/proto-writer.d.ts +30 -0
- package/lib/node/codec/proto-writer.d.ts.map +1 -0
- package/lib/node/codec/proto-writer.js +154 -0
- package/lib/node/codec/proto-writer.js.map +1 -0
- package/lib/node/grpc.spec.d.ts +2 -0
- package/lib/node/grpc.spec.d.ts.map +1 -0
- package/lib/node/grpc.spec.js +148 -0
- package/lib/node/grpc.spec.js.map +1 -0
- package/lib/node/index.d.ts +16 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +32 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/interop.spec.d.ts +2 -0
- package/lib/node/interop.spec.d.ts.map +1 -0
- package/lib/node/interop.spec.js +194 -0
- package/lib/node/interop.spec.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/proto/proto-ast.d.ts +47 -0
- package/lib/node/proto/proto-ast.d.ts.map +1 -0
- package/lib/node/proto/proto-ast.js +3 -0
- package/lib/node/proto/proto-ast.js.map +1 -0
- package/lib/node/proto/proto-parser.d.ts +46 -0
- package/lib/node/proto/proto-parser.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.js +455 -0
- package/lib/node/proto/proto-parser.js.map +1 -0
- package/lib/node/proto/proto-parser.spec.d.ts +2 -0
- package/lib/node/proto/proto-parser.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-parser.spec.js +95 -0
- package/lib/node/proto/proto-parser.spec.js.map +1 -0
- package/lib/node/proto/proto-types.d.ts +77 -0
- package/lib/node/proto/proto-types.d.ts.map +1 -0
- package/lib/node/proto/proto-types.js +449 -0
- package/lib/node/proto/proto-types.js.map +1 -0
- package/lib/node/proto/proto-types.spec.d.ts +2 -0
- package/lib/node/proto/proto-types.spec.d.ts.map +1 -0
- package/lib/node/proto/proto-types.spec.js +120 -0
- package/lib/node/proto/proto-types.spec.js.map +1 -0
- package/lib/node/proto/proto.d.ts +58 -0
- package/lib/node/proto/proto.d.ts.map +1 -0
- package/lib/node/proto/proto.js +178 -0
- package/lib/node/proto/proto.js.map +1 -0
- package/lib/node/proto/well-known.d.ts +6 -0
- package/lib/node/proto/well-known.d.ts.map +1 -0
- package/lib/node/proto/well-known.js +44 -0
- package/lib/node/proto/well-known.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts +22 -0
- package/lib/node/server/grpc-server-lifecycle.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.js +68 -0
- package/lib/node/server/grpc-server-lifecycle.js.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts +2 -0
- package/lib/node/server/grpc-server-lifecycle.spec.d.ts.map +1 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js +100 -0
- package/lib/node/server/grpc-server-lifecycle.spec.js.map +1 -0
- package/lib/node/server/grpc-server.d.ts +61 -0
- package/lib/node/server/grpc-server.d.ts.map +1 -0
- package/lib/node/server/grpc-server.js +316 -0
- package/lib/node/server/grpc-server.js.map +1 -0
- package/lib/node/server/grpc-service.d.ts +25 -0
- package/lib/node/server/grpc-service.d.ts.map +1 -0
- package/lib/node/server/grpc-service.js +42 -0
- package/lib/node/server/grpc-service.js.map +1 -0
- package/lib/node/test/proto-loader.d.ts +3 -0
- package/lib/node/test/proto-loader.d.ts.map +1 -0
- package/lib/node/test/proto-loader.js +16 -0
- package/lib/node/test/proto-loader.js.map +1 -0
- package/lib/node/test/test-protos.d.ts +29 -0
- package/lib/node/test/test-protos.d.ts.map +1 -0
- package/lib/node/test/test-protos.js +150 -0
- package/lib/node/test/test-protos.js.map +1 -0
- package/lib/package.spec.d.ts +2 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +7 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +70 -4
- package/src/node/call/async-queue.ts +79 -0
- package/src/node/call/grpc-status.ts +49 -0
- package/src/node/call/message-framer.ts +109 -0
- package/src/node/call/metadata.ts +85 -0
- package/src/node/client/grpc-channel.ts +327 -0
- package/src/node/codec/proto-reader.ts +172 -0
- package/src/node/codec/proto-writer.ts +170 -0
- package/src/node/grpc.spec.ts +160 -0
- package/src/node/index.ts +15 -0
- package/src/node/interop.spec.ts +174 -0
- package/src/node/module.ts +4 -0
- package/src/node/proto/proto-ast.ts +48 -0
- package/src/node/proto/proto-parser.spec.ts +104 -0
- package/src/node/proto/proto-parser.ts +477 -0
- package/src/node/proto/proto-types.spec.ts +128 -0
- package/src/node/proto/proto-types.ts +497 -0
- package/src/node/proto/proto.ts +218 -0
- package/src/node/proto/well-known.ts +40 -0
- package/src/node/server/grpc-server-lifecycle.spec.ts +88 -0
- package/src/node/server/grpc-server-lifecycle.ts +56 -0
- package/src/node/server/grpc-server.ts +368 -0
- package/src/node/server/grpc-service.ts +40 -0
- package/src/node/test/proto-loader.ts +13 -0
- package/src/node/test/test-protos.ts +159 -0
- package/src/package.spec.ts +5 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const test_protos_1 = require("../test/test-protos");
|
|
5
|
+
const proto_1 = require("./proto");
|
|
6
|
+
const proto_parser_1 = require("./proto-parser");
|
|
7
|
+
(0, bun_test_1.describe)('ProtoParser', () => {
|
|
8
|
+
(0, bun_test_1.it)('reads messages, nested types, oneofs, maps, enums and services', () => {
|
|
9
|
+
var _a;
|
|
10
|
+
const file = proto_parser_1.ProtoParser.parse(test_protos_1.TEST_PROTO);
|
|
11
|
+
(0, bun_test_1.expect)(file.syntax).toBe('proto3');
|
|
12
|
+
(0, bun_test_1.expect)(file.package).toBe('cell.test');
|
|
13
|
+
(0, bun_test_1.expect)(file.imports).toEqual(['google/protobuf/empty.proto', 'google/protobuf/timestamp.proto']);
|
|
14
|
+
(0, bun_test_1.expect)(file.enums[0]).toEqual({
|
|
15
|
+
name: 'Color',
|
|
16
|
+
values: [
|
|
17
|
+
{ name: 'COLOR_UNSPECIFIED', number: 0 },
|
|
18
|
+
{ name: 'RED', number: 1 },
|
|
19
|
+
{ name: 'GREEN', number: 2 },
|
|
20
|
+
],
|
|
21
|
+
});
|
|
22
|
+
const everything = file.messages.find(m => m.name === 'Everything');
|
|
23
|
+
(0, bun_test_1.expect)(everything.oneofs).toEqual(['choice']);
|
|
24
|
+
(0, bun_test_1.expect)(everything.messages.map(m => m.name)).toEqual(['Inner']);
|
|
25
|
+
(0, bun_test_1.expect)(everything.fields.find(f => f.name === 'by_id')).toEqual({ name: 'by_id', number: 6, type: 'Scalars', keyType: 'int64' });
|
|
26
|
+
(0, bun_test_1.expect)(everything.fields.find(f => f.name === 'code')).toEqual({ name: 'code', number: 8, type: 'int32', oneof: 'choice' });
|
|
27
|
+
(0, bun_test_1.expect)((_a = everything.fields.find(f => f.name === 'maybe')) === null || _a === void 0 ? void 0 : _a.rule).toBe('optional');
|
|
28
|
+
(0, bun_test_1.expect)(file.services[0].methods.map(m => [m.name, m.requestStream, m.responseStream])).toEqual([
|
|
29
|
+
['SayHello', false, false],
|
|
30
|
+
['SayHellos', false, true],
|
|
31
|
+
['CollectHellos', true, false],
|
|
32
|
+
['Chat', true, true],
|
|
33
|
+
['Echo', false, false],
|
|
34
|
+
['Nothing', false, false],
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
(0, bun_test_1.it)('skips aggregate options, extensions and extend blocks, and reads proto2 labels and packed', () => {
|
|
38
|
+
const file = proto_parser_1.ProtoParser.parse(`
|
|
39
|
+
syntax = 'proto2';
|
|
40
|
+
package a;
|
|
41
|
+
option (my.opt) = { a: 1 b: { c: "x;" } };
|
|
42
|
+
message M {
|
|
43
|
+
extensions 100 to max;
|
|
44
|
+
required int32 id = 1 [default = -1];
|
|
45
|
+
repeated int32 values = 2 [packed = true, (custom).x = "y"];
|
|
46
|
+
optional string name = 3 [default = "a" "b"];
|
|
47
|
+
}
|
|
48
|
+
extend M { optional int32 extra = 100; }
|
|
49
|
+
service S {
|
|
50
|
+
rpc Get (M) returns (M) { option (google.api.http) = { get: "/v1/{id}" }; };
|
|
51
|
+
}
|
|
52
|
+
`);
|
|
53
|
+
(0, bun_test_1.expect)(file.syntax).toBe('proto2');
|
|
54
|
+
(0, bun_test_1.expect)(file.messages[0].fields).toEqual([
|
|
55
|
+
{ name: 'id', number: 1, type: 'int32', rule: 'required' },
|
|
56
|
+
{ name: 'values', number: 2, type: 'int32', rule: 'repeated', packed: true },
|
|
57
|
+
{ name: 'name', number: 3, type: 'string', rule: 'optional' },
|
|
58
|
+
]);
|
|
59
|
+
(0, bun_test_1.expect)(file.services[0].methods[0]).toEqual({ name: 'Get', requestType: 'M', requestStream: false, responseType: 'M', responseStream: false });
|
|
60
|
+
});
|
|
61
|
+
(0, bun_test_1.it)('reports the line and column of syntax errors', () => {
|
|
62
|
+
(0, bun_test_1.expect)(() => proto_parser_1.ProtoParser.parse('syntax = "proto3";\nmessage M {\n string name = ;\n}', 'm.proto')).toThrow("m.proto:3:17: expected an integer but found ';'");
|
|
63
|
+
(0, bun_test_1.expect)(() => proto_parser_1.ProtoParser.parse('message M { string name = 1;', 'm.proto')).toThrow('unexpected end of file');
|
|
64
|
+
(0, bun_test_1.expect)(() => proto_parser_1.ProtoParser.parse('edition = "2023";')).toThrow('editions are not supported');
|
|
65
|
+
(0, bun_test_1.expect)(() => proto_parser_1.ProtoParser.parse('message M { repeated group G = 1 {} }')).toThrow('groups are not supported');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
(0, bun_test_1.describe)('Proto', () => {
|
|
69
|
+
(0, bun_test_1.it)('resolves type references by protobuf scoping and finds services by full or short name', () => {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
const proto = proto_1.Proto.parse(test_protos_1.TEST_PROTO);
|
|
72
|
+
const greeter = proto.service('Greeter');
|
|
73
|
+
(0, bun_test_1.expect)(proto.service('cell.test.Greeter')).toBe(greeter);
|
|
74
|
+
(0, bun_test_1.expect)(greeter.fullName).toBe('cell.test.Greeter');
|
|
75
|
+
(0, bun_test_1.expect)(greeter.methods.map(m => [m.jsName, m.path])).toContainEqual(['sayHello', '/cell.test.Greeter/SayHello']);
|
|
76
|
+
(0, bun_test_1.expect)((_a = greeter.methods.find(m => m.name === 'Nothing')) === null || _a === void 0 ? void 0 : _a.requestType.fullName).toBe('google.protobuf.Empty');
|
|
77
|
+
const everything = proto.message('Everything');
|
|
78
|
+
(0, bun_test_1.expect)((_b = everything.fields.find(f => f.name === 'inner')) === null || _b === void 0 ? void 0 : _b.messageType).toBe(proto.message('cell.test.Everything.Inner'));
|
|
79
|
+
(0, bun_test_1.expect)((_c = everything.fields.find(f => f.name === 'snakeCaseName')) === null || _c === void 0 ? void 0 : _c.protoName).toBe('snake_case_name');
|
|
80
|
+
(0, bun_test_1.expect)(proto.enum('Color').values).toEqual({ COLOR_UNSPECIFIED: 0, RED: 1, GREEN: 2 });
|
|
81
|
+
});
|
|
82
|
+
(0, bun_test_1.it)('combines several files, resolving types across them', () => {
|
|
83
|
+
const proto = proto_1.Proto.parse('syntax = "proto3"; package app; import "common.proto"; service S { rpc Get (common.Id) returns (.common.Item); }', 'syntax = "proto3"; package common; message Id { int64 id = 1; } message Item { Id id = 1; string name = 2; }');
|
|
84
|
+
const method = proto.service('S').methods[0];
|
|
85
|
+
(0, bun_test_1.expect)([method.requestType.fullName, method.responseType.fullName]).toEqual(['common.Id', 'common.Item']);
|
|
86
|
+
});
|
|
87
|
+
(0, bun_test_1.it)('explains unknown types, missing names and ambiguous ones', () => {
|
|
88
|
+
(0, bun_test_1.expect)(() => proto_1.Proto.parse('syntax = "proto3"; message M { other.Thing t = 1; }')).toThrow("Unknown type other.Thing in M. If it is defined in an imported .proto file, pass that file's source to Proto.parse() too.");
|
|
89
|
+
const proto = proto_1.Proto.parse('syntax = "proto3"; package a; message M {} service S {}', 'syntax = "proto3"; package b; message M {}');
|
|
90
|
+
(0, bun_test_1.expect)(() => proto.message('M')).toThrow("The message name 'M' is ambiguous: a.M, b.M. Use the full name.");
|
|
91
|
+
(0, bun_test_1.expect)(() => proto.service('Missing')).toThrow("No service 'Missing' in the .proto sources. Services: a.S.");
|
|
92
|
+
(0, bun_test_1.expect)(() => proto_1.Proto.parse('syntax = "proto3"; message M {} message M {}')).toThrow('The type M is defined twice');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=proto-parser.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proto-parser.spec.js","sourceRoot":"","sources":["../../../src/node/proto/proto-parser.spec.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAChD,qDAAiD;AACjD,mCAAgC;AAEhC,iDAA6C;AAE7C,IAAA,mBAAQ,EAAC,aAAa,EAAE,GAAG,EAAE;IACzB,IAAA,aAAE,EAAC,gEAAgE,EAAE,GAAG,EAAE;;QACtE,MAAM,IAAI,GAAG,0BAAW,CAAC,KAAK,CAAC,wBAAU,CAAC,CAAC;QAC3C,IAAA,iBAAM,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACnC,IAAA,iBAAM,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QACvC,IAAA,iBAAM,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,6BAA6B,EAAE,iCAAiC,CAAC,CAAC,CAAC;QACjG,IAAA,iBAAM,EAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;YAC1B,IAAI,EAAE,OAAO;YACb,MAAM,EAAE;gBACJ,EAAE,IAAI,EAAE,mBAAmB,EAAE,MAAM,EAAE,CAAC,EAAE;gBACxC,EAAE,IAAI,EAAE,KA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'float' | 'int32' | 'int64' | 'uint32' | 'uint64' | 'sint32' | 'sint64' | 'fixed32' | 'fixed64' | 'sfixed32' | 'sfixed64' | 'bool' | 'string' | 'bytes';
|
|
4
|
+
export type FieldKind = ScalarKind | 'enum' | 'message';
|
|
5
|
+
export declare const SCALAR_KINDS: ReadonlySet<string>;
|
|
6
|
+
/** The type of a value: of a field, or of the value of a map field. */
|
|
7
|
+
export type ValueInfo = {
|
|
8
|
+
readonly kind: ScalarKind;
|
|
9
|
+
readonly messageType?: undefined;
|
|
10
|
+
readonly enumType?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: 'enum';
|
|
13
|
+
readonly enumType: EnumType;
|
|
14
|
+
readonly messageType?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: 'message';
|
|
17
|
+
readonly messageType: MessageType;
|
|
18
|
+
readonly enumType?: undefined;
|
|
19
|
+
};
|
|
20
|
+
export type FieldInfo = ValueInfo & FieldProps;
|
|
21
|
+
export interface FieldProps {
|
|
22
|
+
/** Property name in JavaScript objects: the lowerCamelCase of {@link protoName}. */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly protoName: string;
|
|
25
|
+
readonly number: number;
|
|
26
|
+
readonly repeated: boolean;
|
|
27
|
+
readonly packed: boolean;
|
|
28
|
+
/** Explicit presence (messages, `optional`, oneof members, proto2): left out of decoded objects when not set. */
|
|
29
|
+
readonly presence: boolean;
|
|
30
|
+
readonly oneof?: string;
|
|
31
|
+
readonly map?: {
|
|
32
|
+
readonly key: ScalarKind;
|
|
33
|
+
readonly value: ValueInfo;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** A protobuf enum. Decoded values are names; encoding takes a name or a number. */
|
|
37
|
+
export declare class EnumType {
|
|
38
|
+
readonly fullName: string;
|
|
39
|
+
readonly values: Readonly<Record<string, number>>;
|
|
40
|
+
protected readonly names: Map<number, string>;
|
|
41
|
+
constructor(fullName: string, values: {
|
|
42
|
+
name: string;
|
|
43
|
+
number: number;
|
|
44
|
+
}[]);
|
|
45
|
+
/** The name of a number; the number itself when the enum has no such value. */
|
|
46
|
+
nameOf(number: number): string | number;
|
|
47
|
+
numberOf(value: string | number): number;
|
|
48
|
+
get defaultValue(): string | number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A protobuf message type: encodes plain objects to the binary format and decodes them back.
|
|
52
|
+
*
|
|
53
|
+
* Mapping: field names are lowerCamelCase (`user_id` → `userId`; encoding also takes `user_id`);
|
|
54
|
+
* 64-bit integers decode to decimal strings and encode from strings, numbers or bigints; enums decode to
|
|
55
|
+
* names; `bytes` decode to `Uint8Array` and encode from `Uint8Array` or base64; maps are plain objects.
|
|
56
|
+
* Decoded objects carry the default value of every field without explicit presence, `[]` for repeated
|
|
57
|
+
* fields and `{}` for maps; a set oneof member also sets the oneof's name to the member's name.
|
|
58
|
+
*/
|
|
59
|
+
export declare class MessageType<T = any> {
|
|
60
|
+
readonly fullName: string;
|
|
61
|
+
protected fieldList: FieldInfo[];
|
|
62
|
+
protected byNumber: Map<number, FieldInfo>;
|
|
63
|
+
protected oneofMembers: Map<string, FieldInfo[]>;
|
|
64
|
+
constructor(fullName: string);
|
|
65
|
+
get fields(): readonly FieldInfo[];
|
|
66
|
+
/** Called once by {@link Proto} when every type reference is resolved. */
|
|
67
|
+
setFields(fields: FieldInfo[]): void;
|
|
68
|
+
encode(value: T): Uint8Array;
|
|
69
|
+
decode(bytes: Uint8Array): T;
|
|
70
|
+
/** A message with every field at its default value. */
|
|
71
|
+
create(): T;
|
|
72
|
+
write(value: any, writer: ProtoWriter): void;
|
|
73
|
+
read(reader: ProtoReader, end: number): T;
|
|
74
|
+
protected writeField(field: FieldInfo, value: any, writer: ProtoWriter): void;
|
|
75
|
+
protected readMapEntry(map: NonNullable<FieldInfo['map']>, reader: ProtoReader, target: Record<string, any>): void;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=proto-types.d.ts.map
|
|
@@ -0,0 +1 @@
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AAC,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAUnC;IAED,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,UAAU,CAI3B;IAED,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,CAAC,CAG3B;IAED,uDAAuD;IACvD,MAAM,IAAI,CAAC,CAYV;IAED,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAwB3C;IAED,IAAI,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,CAsCxC;IAED,SAAS,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CA0C5E;IAED,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAgBjH;CACJ"}
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageType = exports.EnumType = exports.SCALAR_KINDS = void 0;
|
|
4
|
+
const proto_reader_1 = require("../codec/proto-reader");
|
|
5
|
+
const proto_writer_1 = require("../codec/proto-writer");
|
|
6
|
+
exports.SCALAR_KINDS = new Set([
|
|
7
|
+
'double',
|
|
8
|
+
'float',
|
|
9
|
+
'int32',
|
|
10
|
+
'int64',
|
|
11
|
+
'uint32',
|
|
12
|
+
'uint64',
|
|
13
|
+
'sint32',
|
|
14
|
+
'sint64',
|
|
15
|
+
'fixed32',
|
|
16
|
+
'fixed64',
|
|
17
|
+
'sfixed32',
|
|
18
|
+
'sfixed64',
|
|
19
|
+
'bool',
|
|
20
|
+
'string',
|
|
21
|
+
'bytes',
|
|
22
|
+
]);
|
|
23
|
+
const LONG_KINDS = new Set(['int64', 'uint64', 'sint64', 'fixed64', 'sfixed64']);
|
|
24
|
+
const WIRE_TYPES = {
|
|
25
|
+
double: 1,
|
|
26
|
+
fixed64: 1,
|
|
27
|
+
sfixed64: 1,
|
|
28
|
+
float: 5,
|
|
29
|
+
fixed32: 5,
|
|
30
|
+
sfixed32: 5,
|
|
31
|
+
int32: 0,
|
|
32
|
+
int64: 0,
|
|
33
|
+
uint32: 0,
|
|
34
|
+
uint64: 0,
|
|
35
|
+
sint32: 0,
|
|
36
|
+
sint64: 0,
|
|
37
|
+
bool: 0,
|
|
38
|
+
enum: 0,
|
|
39
|
+
string: 2,
|
|
40
|
+
bytes: 2,
|
|
41
|
+
message: 2,
|
|
42
|
+
};
|
|
43
|
+
const ONE = BigInt(1);
|
|
44
|
+
const SIXTY_THREE = BigInt(63);
|
|
45
|
+
/** A protobuf enum. Decoded values are names; encoding takes a name or a number. */
|
|
46
|
+
class EnumType {
|
|
47
|
+
constructor(fullName, values) {
|
|
48
|
+
this.fullName = fullName;
|
|
49
|
+
this.names = new Map();
|
|
50
|
+
const map = {};
|
|
51
|
+
for (const { name, number } of values) {
|
|
52
|
+
map[name] = number;
|
|
53
|
+
if (!this.names.has(number)) {
|
|
54
|
+
this.names.set(number, name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
this.values = map;
|
|
58
|
+
}
|
|
59
|
+
/** The name of a number; the number itself when the enum has no such value. */
|
|
60
|
+
nameOf(number) {
|
|
61
|
+
var _a;
|
|
62
|
+
return (_a = this.names.get(number)) !== null && _a !== void 0 ? _a : number;
|
|
63
|
+
}
|
|
64
|
+
numberOf(value) {
|
|
65
|
+
if (typeof value === 'number') {
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
const number = this.values[value];
|
|
69
|
+
if (number === undefined) {
|
|
70
|
+
throw new TypeError(`'${value}' is not a value of enum ${this.fullName} (values: ${Object.keys(this.values).join(', ')})`);
|
|
71
|
+
}
|
|
72
|
+
return number;
|
|
73
|
+
}
|
|
74
|
+
get defaultValue() {
|
|
75
|
+
return this.nameOf(0);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.EnumType = EnumType;
|
|
79
|
+
/**
|
|
80
|
+
* A protobuf message type: encodes plain objects to the binary format and decodes them back.
|
|
81
|
+
*
|
|
82
|
+
* Mapping: field names are lowerCamelCase (`user_id` → `userId`; encoding also takes `user_id`);
|
|
83
|
+
* 64-bit integers decode to decimal strings and encode from strings, numbers or bigints; enums decode to
|
|
84
|
+
* names; `bytes` decode to `Uint8Array` and encode from `Uint8Array` or base64; maps are plain objects.
|
|
85
|
+
* Decoded objects carry the default value of every field without explicit presence, `[]` for repeated
|
|
86
|
+
* fields and `{}` for maps; a set oneof member also sets the oneof's name to the member's name.
|
|
87
|
+
*/
|
|
88
|
+
class MessageType {
|
|
89
|
+
constructor(fullName) {
|
|
90
|
+
this.fullName = fullName;
|
|
91
|
+
this.fieldList = [];
|
|
92
|
+
this.byNumber = new Map();
|
|
93
|
+
this.oneofMembers = new Map();
|
|
94
|
+
}
|
|
95
|
+
get fields() {
|
|
96
|
+
return this.fieldList;
|
|
97
|
+
}
|
|
98
|
+
/** Called once by {@link Proto} when every type reference is resolved. */
|
|
99
|
+
setFields(fields) {
|
|
100
|
+
var _a;
|
|
101
|
+
this.fieldList = fields;
|
|
102
|
+
for (const field of fields) {
|
|
103
|
+
this.byNumber.set(field.number, field);
|
|
104
|
+
if (field.oneof) {
|
|
105
|
+
const members = (_a = this.oneofMembers.get(field.oneof)) !== null && _a !== void 0 ? _a : [];
|
|
106
|
+
members.push(field);
|
|
107
|
+
this.oneofMembers.set(field.oneof, members);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
encode(value) {
|
|
112
|
+
const writer = new proto_writer_1.ProtoWriter();
|
|
113
|
+
this.write(value, writer);
|
|
114
|
+
return writer.finish();
|
|
115
|
+
}
|
|
116
|
+
decode(bytes) {
|
|
117
|
+
const reader = new proto_reader_1.ProtoReader(bytes);
|
|
118
|
+
return this.read(reader, reader.end);
|
|
119
|
+
}
|
|
120
|
+
/** A message with every field at its default value. */
|
|
121
|
+
create() {
|
|
122
|
+
const message = {};
|
|
123
|
+
for (const field of this.fieldList) {
|
|
124
|
+
if (field.map) {
|
|
125
|
+
message[field.name] = {};
|
|
126
|
+
}
|
|
127
|
+
else if (field.repeated) {
|
|
128
|
+
message[field.name] = [];
|
|
129
|
+
}
|
|
130
|
+
else if (!field.presence) {
|
|
131
|
+
message[field.name] = defaultValue(field);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return message;
|
|
135
|
+
}
|
|
136
|
+
write(value, writer) {
|
|
137
|
+
if (typeof value !== 'object' || value === null) {
|
|
138
|
+
throw new TypeError(`${this.fullName} must be an object, got ${value === null ? 'null' : typeof value}`);
|
|
139
|
+
}
|
|
140
|
+
for (const field of this.fieldList) {
|
|
141
|
+
let v = value[field.name];
|
|
142
|
+
if (v === undefined && field.protoName !== field.name) {
|
|
143
|
+
v = value[field.protoName];
|
|
144
|
+
}
|
|
145
|
+
if (v === undefined || v === null) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (field.oneof && typeof value[field.oneof] === 'string' && value[field.oneof] !== field.name) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
this.writeField(field, v, writer);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
if (error instanceof TypeError && !error.message.startsWith(this.fullName)) {
|
|
156
|
+
throw new TypeError(`${this.fullName}.${field.name}: ${error.message}`);
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
read(reader, end) {
|
|
163
|
+
var _a;
|
|
164
|
+
const message = this.create();
|
|
165
|
+
while (reader.pos < end) {
|
|
166
|
+
const tag = reader.uint32();
|
|
167
|
+
const wireType = tag & 7;
|
|
168
|
+
const field = this.byNumber.get(tag >>> 3);
|
|
169
|
+
if (!field) {
|
|
170
|
+
reader.skip(wireType);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (field.map) {
|
|
174
|
+
this.readMapEntry(field.map, reader, message[field.name]);
|
|
175
|
+
}
|
|
176
|
+
else if (field.repeated) {
|
|
177
|
+
const list = message[field.name];
|
|
178
|
+
if (wireType === 2 && WIRE_TYPES[field.kind] !== 2) {
|
|
179
|
+
const packedEnd = reader.uint32() + reader.pos;
|
|
180
|
+
while (reader.pos < packedEnd) {
|
|
181
|
+
list.push(readValue(field, reader));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
list.push(readValue(field, reader));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
message[field.name] = readValue(field, reader);
|
|
190
|
+
if (field.oneof) {
|
|
191
|
+
for (const member of (_a = this.oneofMembers.get(field.oneof)) !== null && _a !== void 0 ? _a : []) {
|
|
192
|
+
if (member !== field) {
|
|
193
|
+
delete message[member.name];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
message[field.oneof] = field.name;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (reader.pos > end) {
|
|
201
|
+
throw new RangeError(`truncated ${this.fullName}`);
|
|
202
|
+
}
|
|
203
|
+
return message;
|
|
204
|
+
}
|
|
205
|
+
writeField(field, value, writer) {
|
|
206
|
+
if (field.map) {
|
|
207
|
+
if (typeof value !== 'object') {
|
|
208
|
+
throw new TypeError(`expected an object for a map, got ${typeof value}`);
|
|
209
|
+
}
|
|
210
|
+
const { key, value: valueInfo } = field.map;
|
|
211
|
+
const entries = value instanceof Map ? [...value.entries()] : Object.entries(value);
|
|
212
|
+
for (const [k, v] of entries) {
|
|
213
|
+
writer.tag(field.number, 2);
|
|
214
|
+
const start = writer.fork();
|
|
215
|
+
writer.tag(1, WIRE_TYPES[key]);
|
|
216
|
+
writeValue({ kind: key }, mapKey(key, k), writer);
|
|
217
|
+
if (v !== undefined && v !== null) {
|
|
218
|
+
writer.tag(2, WIRE_TYPES[valueInfo.kind]);
|
|
219
|
+
writeValue(valueInfo, v, writer);
|
|
220
|
+
}
|
|
221
|
+
writer.ldelim(start);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (field.repeated) {
|
|
225
|
+
if (!Array.isArray(value)) {
|
|
226
|
+
throw new TypeError(`expected an array, got ${typeof value}`);
|
|
227
|
+
}
|
|
228
|
+
if (field.packed && WIRE_TYPES[field.kind] !== 2) {
|
|
229
|
+
if (value.length > 0) {
|
|
230
|
+
writer.tag(field.number, 2);
|
|
231
|
+
const start = writer.fork();
|
|
232
|
+
for (const item of value) {
|
|
233
|
+
writeValue(field, item, writer);
|
|
234
|
+
}
|
|
235
|
+
writer.ldelim(start);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const wireType = WIRE_TYPES[field.kind];
|
|
240
|
+
for (const item of value) {
|
|
241
|
+
writer.tag(field.number, wireType);
|
|
242
|
+
writeValue(field, item, writer);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
else if (field.presence || !isDefault(field, value)) {
|
|
247
|
+
writer.tag(field.number, WIRE_TYPES[field.kind]);
|
|
248
|
+
writeValue(field, value, writer);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
readMapEntry(map, reader, target) {
|
|
252
|
+
const { key: keyKind, value: valueInfo } = map;
|
|
253
|
+
const end = reader.uint32() + reader.pos;
|
|
254
|
+
let key = defaultValue({ kind: keyKind });
|
|
255
|
+
let value;
|
|
256
|
+
while (reader.pos < end) {
|
|
257
|
+
const tag = reader.uint32();
|
|
258
|
+
if (tag >>> 3 === 1) {
|
|
259
|
+
key = readValue({ kind: keyKind }, reader);
|
|
260
|
+
}
|
|
261
|
+
else if (tag >>> 3 === 2) {
|
|
262
|
+
value = readValue(valueInfo, reader);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
reader.skip(tag & 7);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
target[String(key)] = value !== null && value !== void 0 ? value : (valueInfo.kind === 'message' ? valueInfo.messageType.create() : defaultValue(valueInfo));
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
exports.MessageType = MessageType;
|
|
272
|
+
function defaultValue(info) {
|
|
273
|
+
switch (info.kind) {
|
|
274
|
+
case 'string':
|
|
275
|
+
return '';
|
|
276
|
+
case 'bool':
|
|
277
|
+
return false;
|
|
278
|
+
case 'bytes':
|
|
279
|
+
return new Uint8Array(0);
|
|
280
|
+
case 'enum':
|
|
281
|
+
return info.enumType.defaultValue;
|
|
282
|
+
case 'message':
|
|
283
|
+
return undefined;
|
|
284
|
+
default:
|
|
285
|
+
return LONG_KINDS.has(info.kind) ? '0' : 0;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function isDefault(info, value) {
|
|
289
|
+
switch (info.kind) {
|
|
290
|
+
case 'string':
|
|
291
|
+
return value === '';
|
|
292
|
+
case 'bool':
|
|
293
|
+
return value === false;
|
|
294
|
+
case 'bytes':
|
|
295
|
+
return value.length === 0;
|
|
296
|
+
case 'enum':
|
|
297
|
+
return info.enumType.numberOf(value) === 0;
|
|
298
|
+
case 'message':
|
|
299
|
+
return false;
|
|
300
|
+
default:
|
|
301
|
+
return typeof value === 'bigint' ? value === BigInt(0) : Number(value) === 0;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
function mapKey(kind, key) {
|
|
305
|
+
if (kind === 'bool') {
|
|
306
|
+
return key === true || key === 'true';
|
|
307
|
+
}
|
|
308
|
+
if (kind === 'string' || LONG_KINDS.has(kind)) {
|
|
309
|
+
return typeof key === 'number' ? String(key) : key;
|
|
310
|
+
}
|
|
311
|
+
return Number(key);
|
|
312
|
+
}
|
|
313
|
+
function toBigInt(value) {
|
|
314
|
+
if (typeof value === 'bigint') {
|
|
315
|
+
return value;
|
|
316
|
+
}
|
|
317
|
+
if (typeof value === 'number' && !Number.isInteger(value)) {
|
|
318
|
+
throw new TypeError(`${value} is not an integer`);
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
return BigInt(value);
|
|
322
|
+
}
|
|
323
|
+
catch (_a) {
|
|
324
|
+
throw new TypeError(`${JSON.stringify(value)} is not an integer`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function toNumber(value) {
|
|
328
|
+
const number = typeof value === 'number' ? value : Number(value);
|
|
329
|
+
if (Number.isNaN(number) && !(typeof value === 'number' || value === 'NaN')) {
|
|
330
|
+
throw new TypeError(`${JSON.stringify(value)} is not a number`);
|
|
331
|
+
}
|
|
332
|
+
return number;
|
|
333
|
+
}
|
|
334
|
+
function toBytes(value) {
|
|
335
|
+
if (value instanceof Uint8Array) {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
if (typeof value === 'string') {
|
|
339
|
+
return Buffer.from(value, 'base64');
|
|
340
|
+
}
|
|
341
|
+
if (ArrayBuffer.isView(value)) {
|
|
342
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
343
|
+
}
|
|
344
|
+
if (value instanceof ArrayBuffer) {
|
|
345
|
+
return new Uint8Array(value);
|
|
346
|
+
}
|
|
347
|
+
throw new TypeError(`expected a Uint8Array or a base64 string, got ${typeof value}`);
|
|
348
|
+
}
|
|
349
|
+
function writeValue(info, value, writer) {
|
|
350
|
+
switch (info.kind) {
|
|
351
|
+
case 'string':
|
|
352
|
+
writer.string(typeof value === 'string' ? value : String(value));
|
|
353
|
+
break;
|
|
354
|
+
case 'bool':
|
|
355
|
+
writer.bool(Boolean(value));
|
|
356
|
+
break;
|
|
357
|
+
case 'int32':
|
|
358
|
+
writer.int32(toNumber(value) | 0);
|
|
359
|
+
break;
|
|
360
|
+
case 'uint32':
|
|
361
|
+
writer.uint32(toNumber(value));
|
|
362
|
+
break;
|
|
363
|
+
case 'sint32':
|
|
364
|
+
writer.sint32(toNumber(value) | 0);
|
|
365
|
+
break;
|
|
366
|
+
case 'fixed32':
|
|
367
|
+
writer.fixed32(toNumber(value));
|
|
368
|
+
break;
|
|
369
|
+
case 'sfixed32':
|
|
370
|
+
writer.sfixed32(toNumber(value));
|
|
371
|
+
break;
|
|
372
|
+
case 'double':
|
|
373
|
+
writer.double(toNumber(value));
|
|
374
|
+
break;
|
|
375
|
+
case 'float':
|
|
376
|
+
writer.float(toNumber(value));
|
|
377
|
+
break;
|
|
378
|
+
case 'int64':
|
|
379
|
+
case 'uint64':
|
|
380
|
+
writer.varint64(BigInt.asUintN(64, toBigInt(value)));
|
|
381
|
+
break;
|
|
382
|
+
case 'sint64': {
|
|
383
|
+
const v = BigInt.asIntN(64, toBigInt(value));
|
|
384
|
+
writer.varint64(BigInt.asUintN(64, (v << ONE) ^ (v >> SIXTY_THREE)));
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case 'fixed64':
|
|
388
|
+
writer.fixed64(BigInt.asUintN(64, toBigInt(value)));
|
|
389
|
+
break;
|
|
390
|
+
case 'sfixed64':
|
|
391
|
+
writer.sfixed64(BigInt.asIntN(64, toBigInt(value)));
|
|
392
|
+
break;
|
|
393
|
+
case 'bytes':
|
|
394
|
+
writer.bytes(toBytes(value));
|
|
395
|
+
break;
|
|
396
|
+
case 'enum':
|
|
397
|
+
writer.int32(info.enumType.numberOf(value));
|
|
398
|
+
break;
|
|
399
|
+
case 'message': {
|
|
400
|
+
const start = writer.fork();
|
|
401
|
+
info.messageType.write(value, writer);
|
|
402
|
+
writer.ldelim(start);
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function readValue(info, reader) {
|
|
408
|
+
switch (info.kind) {
|
|
409
|
+
case 'string':
|
|
410
|
+
return reader.string();
|
|
411
|
+
case 'bool':
|
|
412
|
+
return reader.bool();
|
|
413
|
+
case 'int32':
|
|
414
|
+
return reader.int32();
|
|
415
|
+
case 'uint32':
|
|
416
|
+
return reader.uint32();
|
|
417
|
+
case 'sint32':
|
|
418
|
+
return reader.sint32();
|
|
419
|
+
case 'fixed32':
|
|
420
|
+
return reader.fixed32();
|
|
421
|
+
case 'sfixed32':
|
|
422
|
+
return reader.sfixed32();
|
|
423
|
+
case 'double':
|
|
424
|
+
return reader.double();
|
|
425
|
+
case 'float':
|
|
426
|
+
return reader.float();
|
|
427
|
+
case 'int64':
|
|
428
|
+
return BigInt.asIntN(64, reader.varint64()).toString();
|
|
429
|
+
case 'uint64':
|
|
430
|
+
return reader.varint64().toString();
|
|
431
|
+
case 'sint64': {
|
|
432
|
+
const v = reader.varint64();
|
|
433
|
+
return ((v >> ONE) ^ -(v & ONE)).toString();
|
|
434
|
+
}
|
|
435
|
+
case 'fixed64':
|
|
436
|
+
return reader.fixed64().toString();
|
|
437
|
+
case 'sfixed64':
|
|
438
|
+
return reader.sfixed64().toString();
|
|
439
|
+
case 'bytes':
|
|
440
|
+
return reader.bytes();
|
|
441
|
+
case 'enum':
|
|
442
|
+
return info.enumType.nameOf(reader.int32());
|
|
443
|
+
case 'message': {
|
|
444
|
+
const end = reader.uint32() + reader.pos;
|
|
445
|
+
return info.messageType.read(reader, end);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
//# sourceMappingURL=proto-types.js.map
|