@aptre/protobuf-es-lite 0.1.6 → 0.2.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 +4 -1
- package/dist/binary.js +5 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -1
- package/dist/is-message.d.ts +6 -2
- package/dist/is-message.js +2 -1
- package/dist/message.d.ts +34 -19
- package/dist/message.js +3 -0
- package/dist/partial.d.ts +2 -2
- package/dist/typescript.js +19 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -52,9 +52,12 @@ This fork generates the ts-proto style with the protoc-gen-es tools:
|
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
54
|
const myMessage: MyMessage = {body: "Hello world"}
|
|
55
|
-
const
|
|
55
|
+
const myCompleteMessage: CompleteMessage<MyMessage> = MyMessage.create(myMessage)
|
|
56
|
+
const myMessageBin = MyMessage.toBinary(myCompleteMessage)
|
|
56
57
|
```
|
|
57
58
|
|
|
59
|
+
**Note that the default Message is equivalent to PartialMessage<T> from protobuf-es.**
|
|
60
|
+
|
|
58
61
|
## Installation
|
|
59
62
|
|
|
60
63
|
`protoc-gen-es` generates base types - messages and enumerations - from your
|
package/dist/binary.js
CHANGED
|
@@ -293,12 +293,12 @@ function writeMessageField(writer, options, field, value) {
|
|
|
293
293
|
if (field.delimited)
|
|
294
294
|
writer
|
|
295
295
|
.tag(field.no, protobuf_1.WireType.StartGroup)
|
|
296
|
-
.raw(
|
|
296
|
+
.raw(field.T.toBinary(message, options))
|
|
297
297
|
.tag(field.no, protobuf_1.WireType.EndGroup);
|
|
298
298
|
else
|
|
299
299
|
writer
|
|
300
300
|
.tag(field.no, protobuf_1.WireType.LengthDelimited)
|
|
301
|
-
.bytes(
|
|
301
|
+
.bytes(field.T.toBinary(message, options));
|
|
302
302
|
}
|
|
303
303
|
function writeScalar(writer, type, fieldNo, value) {
|
|
304
304
|
(0, assert_js_1.assert)(value !== undefined);
|
|
@@ -381,7 +381,9 @@ function writeMapEntry(writer, options, field, key, value) {
|
|
|
381
381
|
break;
|
|
382
382
|
case "message":
|
|
383
383
|
(0, assert_js_1.assert)(value !== undefined);
|
|
384
|
-
writer
|
|
384
|
+
writer
|
|
385
|
+
.tag(2, protobuf_1.WireType.LengthDelimited)
|
|
386
|
+
.bytes(field.V.T.toBinary(value, options));
|
|
385
387
|
break;
|
|
386
388
|
}
|
|
387
389
|
writer.join();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { Message,
|
|
1
|
+
export { Message, CompleteMessage, AnyMessage, MessageType, Field, CompleteField, compareMessages, createMessageType, } from "./message.js";
|
|
2
|
+
export { isCompleteMessage, isCompleteField } from "./is-message.js";
|
|
2
3
|
export { newFieldList, FieldList, PartialFieldInfo, FieldInfo, OneofInfo, fieldJsonName, localFieldName, localOneofName, } from "./field.js";
|
|
3
4
|
export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.createMessageType = exports.compareMessages = void 0;
|
|
3
|
+
exports.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = exports.localOneofName = exports.localFieldName = exports.fieldJsonName = exports.FieldList = exports.newFieldList = exports.isCompleteField = exports.isCompleteMessage = exports.createMessageType = exports.compareMessages = void 0;
|
|
4
4
|
var message_js_1 = require("./message.js");
|
|
5
5
|
Object.defineProperty(exports, "compareMessages", { enumerable: true, get: function () { return message_js_1.compareMessages; } });
|
|
6
6
|
Object.defineProperty(exports, "createMessageType", { enumerable: true, get: function () { return message_js_1.createMessageType; } });
|
|
7
|
+
var is_message_js_1 = require("./is-message.js");
|
|
8
|
+
Object.defineProperty(exports, "isCompleteMessage", { enumerable: true, get: function () { return is_message_js_1.isCompleteMessage; } });
|
|
9
|
+
Object.defineProperty(exports, "isCompleteField", { enumerable: true, get: function () { return is_message_js_1.isCompleteField; } });
|
|
7
10
|
var field_js_1 = require("./field.js");
|
|
8
11
|
Object.defineProperty(exports, "newFieldList", { enumerable: true, get: function () { return field_js_1.newFieldList; } });
|
|
9
12
|
Object.defineProperty(exports, "FieldList", { enumerable: true, get: function () { return field_js_1.FieldList; } });
|
package/dist/is-message.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { FieldInfo } from "./field.js";
|
|
2
|
-
import { AnyMessage, Message,
|
|
2
|
+
import { AnyMessage, CompleteMessage, Message, Field, CompleteField } from "./message.js";
|
|
3
3
|
/**
|
|
4
4
|
* Check whether the given partial has all fields present recursively.
|
|
5
5
|
*/
|
|
6
|
-
export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg:
|
|
6
|
+
export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg: Message<T>, fields: readonly FieldInfo[]): arg is CompleteMessage<T>;
|
|
7
|
+
/**
|
|
8
|
+
* Check whether the given partial field has a full value present recursively.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isCompleteField<F>(value: Field<F>, field: FieldInfo): value is CompleteField<F>;
|
package/dist/is-message.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCompleteMessage = void 0;
|
|
3
|
+
exports.isCompleteField = exports.isCompleteMessage = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Check whether the given partial has all fields present recursively.
|
|
6
6
|
*/
|
|
@@ -59,3 +59,4 @@ function isCompleteField(value, field) {
|
|
|
59
59
|
return fieldKind;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
+
exports.isCompleteField = isCompleteField;
|
package/dist/message.d.ts
CHANGED
|
@@ -1,42 +1,57 @@
|
|
|
1
1
|
import { BinaryReadOptions, JsonReadOptions, JsonValue, MessageType as BufMessageType, BinaryWriteOptions, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
|
|
2
2
|
import { FieldList, FieldListSource } from "./field.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* PartialMessage<T> constructs a type from a message. The resulting type
|
|
5
|
+
* only contains the protobuf field members of the message, and all of them
|
|
6
|
+
* are optional.
|
|
7
|
+
*
|
|
8
|
+
* Note that the optionality of the fields is the only difference between
|
|
9
|
+
* PartialMessage and PlainMessage.
|
|
10
|
+
*
|
|
11
|
+
* PartialMessage is similar to the built-in type Partial<T>, but recursive,
|
|
12
|
+
* and respects `oneof` groups.
|
|
5
13
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
14
|
+
export type Message<T extends Message<T>> = {
|
|
15
|
+
[k: string]: Field<any>;
|
|
16
|
+
};
|
|
8
17
|
/**
|
|
9
18
|
* AnyMessage is an interface implemented by all messages. If you need to
|
|
10
19
|
* handle messages of unknown type, this interface provides a convenient
|
|
11
20
|
* index signature to access fields with message["fieldname"].
|
|
12
21
|
*/
|
|
13
22
|
export interface AnyMessage extends Message<AnyMessage> {
|
|
14
|
-
[k: string]: any
|
|
23
|
+
[k: string]: Field<any>;
|
|
15
24
|
}
|
|
25
|
+
export type Field<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? Message<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
26
|
+
case: C;
|
|
27
|
+
value: Message<V>;
|
|
28
|
+
} : F extends {
|
|
29
|
+
case: string | undefined;
|
|
30
|
+
value?: unknown;
|
|
31
|
+
} ? F : F extends {
|
|
32
|
+
[key: string | number]: CompleteMessage<infer U>;
|
|
33
|
+
} ? {
|
|
34
|
+
[key: string | number]: Message<U>;
|
|
35
|
+
} : F;
|
|
16
36
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* are optional.
|
|
20
|
-
*
|
|
21
|
-
* Note that the optionality of the fields is the only difference between
|
|
22
|
-
* PartialMessage and PlainMessage.
|
|
37
|
+
* CompleteMessage<T> constructs a type from a message which requires all fields
|
|
38
|
+
* be present in the object recursively (including zero values if unset).
|
|
23
39
|
*
|
|
24
|
-
*
|
|
25
|
-
* and respects `oneof` groups.
|
|
40
|
+
* This type corresponds to Message in protobuf-es.
|
|
26
41
|
*/
|
|
27
|
-
export type
|
|
28
|
-
[P in keyof T as T[P] extends Function ? never : P]?:
|
|
42
|
+
export type CompleteMessage<T extends Message<T>> = {
|
|
43
|
+
[P in keyof T as T[P] extends Function ? never : P]?: CompleteField<T[P]>;
|
|
29
44
|
};
|
|
30
|
-
export type
|
|
45
|
+
export type CompleteField<F> = F extends Date | Uint8Array | bigint | boolean | string | number ? F : F extends Array<infer U> ? Array<Field<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<Field<U>> : F extends CompleteMessage<infer U> ? CompleteMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
31
46
|
case: C;
|
|
32
|
-
value:
|
|
47
|
+
value: CompleteMessage<V>;
|
|
33
48
|
} : F extends {
|
|
34
49
|
case: string | undefined;
|
|
35
50
|
value?: unknown;
|
|
36
51
|
} ? F : F extends {
|
|
37
|
-
[key: string | number]:
|
|
52
|
+
[key: string | number]: CompleteMessage<infer U>;
|
|
38
53
|
} ? {
|
|
39
|
-
[key: string | number]:
|
|
54
|
+
[key: string | number]: CompleteMessage<U>;
|
|
40
55
|
} : F;
|
|
41
56
|
type OneofSelectedMessage<K extends string, M extends Message<M>> = {
|
|
42
57
|
case: K;
|
|
@@ -60,7 +75,7 @@ export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<Buf
|
|
|
60
75
|
/**
|
|
61
76
|
* Create a new instance of this message with zero values for fields.
|
|
62
77
|
*/
|
|
63
|
-
create(partial?:
|
|
78
|
+
create(partial?: Message<T>): T;
|
|
64
79
|
/**
|
|
65
80
|
* Create a deep copy.
|
|
66
81
|
*/
|
package/dist/message.js
CHANGED
|
@@ -22,6 +22,9 @@ const binary_js_1 = require("./binary.js");
|
|
|
22
22
|
const json_js_1 = require("./json.js");
|
|
23
23
|
// compareMessages compares two messages for equality.
|
|
24
24
|
function compareMessages(fields, a, b) {
|
|
25
|
+
if (a == null && b == null) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
25
28
|
if (a === b) {
|
|
26
29
|
return true;
|
|
27
30
|
}
|
package/dist/partial.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { FieldList } from "./field.js";
|
|
2
|
-
import { Message
|
|
3
|
-
export declare function applyPartialMessage<T extends Message<T>>(source:
|
|
2
|
+
import { Message } from "./message.js";
|
|
3
|
+
export declare function applyPartialMessage<T extends Message<T>>(source: Message<T> | undefined, target: Message<T>, fields: FieldList): void;
|
package/dist/typescript.js
CHANGED
|
@@ -80,7 +80,6 @@ function topologicalSort(messages, dependencies) {
|
|
|
80
80
|
}
|
|
81
81
|
return result;
|
|
82
82
|
}
|
|
83
|
-
// prettier-ignore
|
|
84
83
|
function generateEnum(f, enumeration) {
|
|
85
84
|
f.print(f.jsDoc(enumeration));
|
|
86
85
|
f.print(f.exportDecl("enum", enumeration), " {");
|
|
@@ -101,16 +100,13 @@ function generateEnum(f, enumeration) {
|
|
|
101
100
|
f.print("};");
|
|
102
101
|
f.print();
|
|
103
102
|
}
|
|
104
|
-
// prettier-ignore
|
|
105
103
|
function generateMessage(schema, f, message) {
|
|
106
104
|
// check if we support this runtime
|
|
107
105
|
(0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
108
106
|
f.print(f.jsDoc(message));
|
|
109
107
|
f.print(f.exportDecl("interface", message), " extends ", MessageImport, "<", message, "> {");
|
|
110
108
|
for (const field of message.fields) {
|
|
111
|
-
|
|
112
|
-
generateField(f, field);
|
|
113
|
-
}
|
|
109
|
+
generateField(f, field);
|
|
114
110
|
}
|
|
115
111
|
for (const oneof of message.oneofs) {
|
|
116
112
|
generateOneof(f, oneof);
|
|
@@ -137,29 +133,38 @@ function generateMessage(schema, f, message) {
|
|
|
137
133
|
generateMessage(schema, f, nestedMessage);
|
|
138
134
|
}
|
|
139
135
|
}
|
|
140
|
-
// prettier-ignore
|
|
141
136
|
function generateField(f, field) {
|
|
137
|
+
if (field.oneof) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
142
140
|
f.print(f.jsDoc(field, " "));
|
|
143
141
|
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
144
|
-
f.print(" ", (0, ecmascript_1.localName)(field), "
|
|
142
|
+
f.print(" ", (0, ecmascript_1.localName)(field), "?: ", typing, ";");
|
|
145
143
|
}
|
|
146
|
-
// prettier-ignore
|
|
147
144
|
function generateOneof(f, oneof) {
|
|
148
145
|
f.print();
|
|
149
146
|
f.print(f.jsDoc(oneof, " "));
|
|
150
|
-
var oneOfCases = oneof.fields
|
|
147
|
+
var oneOfCases = oneof.fields
|
|
148
|
+
.map((field) => {
|
|
151
149
|
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
152
150
|
const doc = f.jsDoc(field, " ");
|
|
153
|
-
return [
|
|
154
|
-
|
|
155
|
-
|
|
151
|
+
return [
|
|
152
|
+
` | {\n`,
|
|
153
|
+
doc,
|
|
154
|
+
`\n value: `,
|
|
155
|
+
typing,
|
|
156
|
+
`;\n case: "`,
|
|
157
|
+
(0, ecmascript_1.localName)(field),
|
|
158
|
+
`";\n }`,
|
|
159
|
+
];
|
|
160
|
+
})
|
|
161
|
+
.flat();
|
|
162
|
+
f.print(" ", oneof.name, "?: {\n value?: undefined,\n case: undefined\n }", oneOfCases, ";");
|
|
156
163
|
}
|
|
157
|
-
// prettier-ignore
|
|
158
164
|
function generateFieldInfo(f, field) {
|
|
159
165
|
f.print(" ", getFieldInfoLiteral(field), ",");
|
|
160
166
|
}
|
|
161
167
|
exports.generateFieldInfo = generateFieldInfo;
|
|
162
|
-
// prettier-ignore
|
|
163
168
|
function getFieldInfoLiteral(field) {
|
|
164
169
|
const e = [];
|
|
165
170
|
e.push("{ no: ", field.number, `, `);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aptre/protobuf-es-lite",
|
|
3
3
|
"description": "Lightweight Protobuf codegen for TypeScript and JavaScript.",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"build": "npm run clean && tsc --project tsconfig.json --outDir ./dist",
|
|
37
37
|
"release:version": "npm version patch -m \"release: v%s\" --no-git-tag-version",
|
|
38
38
|
"release:commit": "git reset && git add package.json && git commit -s -m \"release: v$npm_package_version\" && git tag v$npm_package_version",
|
|
39
|
-
"release:publish": "git push --
|
|
39
|
+
"release:publish": "git push && git push --tags && npm run build && npm publish",
|
|
40
40
|
"release": "npm run release:version && npm run release:commit",
|
|
41
41
|
"typecheck": "tsc --noEmit --project tsconfig.json --outDir ./dist",
|
|
42
42
|
"example": "npm run build && protoc --plugin=./bin/protoc-gen-es-lite --es-lite_out=. --es-lite_opt target=ts --es-lite_opt ts_nocheck=false ./example/example.proto",
|