@aptre/protobuf-es-lite 0.3.1 → 0.4.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/dist/binary.d.ts +1 -1
- package/dist/binary.js +11 -15
- package/dist/enum.d.ts +11 -1
- package/dist/enum.js +56 -7
- package/dist/field-wrapper.d.ts +13 -0
- package/dist/field-wrapper.js +7 -0
- package/dist/field.d.ts +38 -12
- package/dist/field.js +9 -5
- package/dist/google/protobuf/timestamp.pb.d.ts +2 -1
- package/dist/google/protobuf/timestamp.pb.js +12 -0
- package/dist/google/protobuf/wrappers.pb.js +9 -9
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/json.js +44 -32
- package/dist/message.d.ts +8 -4
- package/dist/message.js +44 -105
- package/dist/names.d.ts +2 -0
- package/dist/names.js +13 -20
- package/dist/partial.d.ts +3 -2
- package/dist/partial.js +164 -67
- package/dist/protoc-gen-es-lite/typescript.js +34 -2
- package/dist/scalar.d.ts +9 -0
- package/dist/scalar.js +24 -0
- package/dist/util.d.ts +1 -1
- package/dist/util.js +2 -2
- package/dist/zero-value.d.ts +23 -0
- package/dist/zero-value.js +88 -0
- package/example/tsconfig.json +1 -3
- package/package.json +11 -9
package/dist/scalar.js
CHANGED
|
@@ -161,3 +161,27 @@ export function isScalarZeroValue(type, value) {
|
|
|
161
161
|
return value == 0; // Loose comparison matches 0n, 0 and "0"
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns the normalized version of the scalar value.
|
|
166
|
+
* Zero or null is cast to the zero value.
|
|
167
|
+
* Bytes is cast to a Uint8Array.
|
|
168
|
+
* The BigInt long type is used.
|
|
169
|
+
* If clone is set, Uint8Array will always be copied to a new value.
|
|
170
|
+
*/
|
|
171
|
+
export function normalizeScalarValue(type, value, clone, longType = LongType.BIGINT) {
|
|
172
|
+
if (value == null) {
|
|
173
|
+
return scalarZeroValue(type, longType);
|
|
174
|
+
}
|
|
175
|
+
if (type === ScalarType.BYTES) {
|
|
176
|
+
return toU8Arr(value, clone);
|
|
177
|
+
}
|
|
178
|
+
if (isScalarZeroValue(type, value)) {
|
|
179
|
+
return scalarZeroValue(type, longType);
|
|
180
|
+
}
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
// converts any ArrayLike<number> to Uint8Array if necessary.
|
|
184
|
+
// if clone is set, force clones the array to a copy.
|
|
185
|
+
export function toU8Arr(input, clone) {
|
|
186
|
+
return !clone && input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
187
|
+
}
|
package/dist/util.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export declare function getFieldDefaultValueExpression(field: DescField | DescEx
|
|
|
15
15
|
*
|
|
16
16
|
* Returns either:
|
|
17
17
|
* - empty array literal for repeated fields
|
|
18
|
-
* -
|
|
18
|
+
* - Object.create(null) for maps
|
|
19
19
|
* - undefined for message fields
|
|
20
20
|
* - an enums first value
|
|
21
21
|
* - scalar zero value
|
package/dist/util.js
CHANGED
|
@@ -134,7 +134,7 @@ export function getFieldDefaultValueExpression(field, enumAs = "enum_value_as_is
|
|
|
134
134
|
*
|
|
135
135
|
* Returns either:
|
|
136
136
|
* - empty array literal for repeated fields
|
|
137
|
-
* -
|
|
137
|
+
* - Object.create(null) for maps
|
|
138
138
|
* - undefined for message fields
|
|
139
139
|
* - an enums first value
|
|
140
140
|
* - scalar zero value
|
|
@@ -147,7 +147,7 @@ export function getFieldZeroValueExpression(field, enumAs = "enum_value_as_is")
|
|
|
147
147
|
case "message":
|
|
148
148
|
return undefined;
|
|
149
149
|
case "map":
|
|
150
|
-
return "
|
|
150
|
+
return "Object.create(null)";
|
|
151
151
|
case "enum": {
|
|
152
152
|
// In proto3, the first enum value must be zero.
|
|
153
153
|
// In proto2, protobuf-go returns the first value as the default.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DescExtension, DescField } from "./descriptor-set.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the zero value for a field.
|
|
4
|
+
*
|
|
5
|
+
* Returns either:
|
|
6
|
+
* - empty array literal for repeated fields
|
|
7
|
+
* - Object.create(null) for maps
|
|
8
|
+
* - undefined for message fields
|
|
9
|
+
* - an enums first value
|
|
10
|
+
* - scalar zero value
|
|
11
|
+
*/
|
|
12
|
+
export declare function getFieldZeroValue(field: DescField | DescExtension): any;
|
|
13
|
+
/**
|
|
14
|
+
* Returns true for a zero-value (all fields are zero).
|
|
15
|
+
*
|
|
16
|
+
* In proto3, zero-values are not written to the wire, unless the field is
|
|
17
|
+
* optional or repeated.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isMessageZeroValue<T extends Record<string, any>>(value: T | null | undefined, fields: (DescField | DescExtension)[]): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Compares if the value is considered a zero value for the field.
|
|
22
|
+
*/
|
|
23
|
+
export declare function compareFieldZeroValue<T>(field: DescField | DescExtension, value: T | null | undefined): boolean;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { enumDescZeroValue } from "./enum.js";
|
|
2
|
+
import { localName } from "./names.js";
|
|
3
|
+
import { scalarZeroValue } from "./scalar.js";
|
|
4
|
+
/**
|
|
5
|
+
* Returns the zero value for a field.
|
|
6
|
+
*
|
|
7
|
+
* Returns either:
|
|
8
|
+
* - empty array literal for repeated fields
|
|
9
|
+
* - Object.create(null) for maps
|
|
10
|
+
* - undefined for message fields
|
|
11
|
+
* - an enums first value
|
|
12
|
+
* - scalar zero value
|
|
13
|
+
*/
|
|
14
|
+
export function getFieldZeroValue(field) {
|
|
15
|
+
if (field.repeated) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
switch (field.fieldKind) {
|
|
19
|
+
case "message":
|
|
20
|
+
return undefined;
|
|
21
|
+
case "map":
|
|
22
|
+
return Object.create(null);
|
|
23
|
+
case "enum": {
|
|
24
|
+
return enumDescZeroValue(field.enum);
|
|
25
|
+
}
|
|
26
|
+
case "scalar": {
|
|
27
|
+
return scalarZeroValue(field.scalar, field.longType);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns true for a zero-value (all fields are zero).
|
|
33
|
+
*
|
|
34
|
+
* In proto3, zero-values are not written to the wire, unless the field is
|
|
35
|
+
* optional or repeated.
|
|
36
|
+
*/
|
|
37
|
+
export function isMessageZeroValue(value, fields) {
|
|
38
|
+
if (value == null) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (typeof value !== "object") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
for (const field of fields) {
|
|
45
|
+
const fieldLocalName = localName(field);
|
|
46
|
+
if (fieldLocalName in value &&
|
|
47
|
+
!compareFieldZeroValue(field, value[fieldLocalName])) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Compares if the value is considered a zero value for the field.
|
|
55
|
+
*/
|
|
56
|
+
export function compareFieldZeroValue(field, value) {
|
|
57
|
+
if (value == null || value == 0) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (field.repeated) {
|
|
61
|
+
if (!Array.isArray(value) || typeof value.length !== "number") {
|
|
62
|
+
throw new Error("invalid repeated field: must be an array");
|
|
63
|
+
}
|
|
64
|
+
return value.length === 0;
|
|
65
|
+
}
|
|
66
|
+
switch (field.fieldKind) {
|
|
67
|
+
case "message":
|
|
68
|
+
if (typeof value !== "object") {
|
|
69
|
+
throw new Error("invalid message: must be an object");
|
|
70
|
+
}
|
|
71
|
+
// We need to check if all the fields are empty.
|
|
72
|
+
return isMessageZeroValue(value, field.message.fields);
|
|
73
|
+
case "map":
|
|
74
|
+
return Object.create(null);
|
|
75
|
+
case "enum": {
|
|
76
|
+
// In proto3, the first enum value must be zero.
|
|
77
|
+
// In proto2, protobuf-go returns the first value as the default.
|
|
78
|
+
if (field.enum.values.length < 1) {
|
|
79
|
+
throw new Error("invalid enum: missing at least one value");
|
|
80
|
+
}
|
|
81
|
+
const zeroValue = field.enum.values[0];
|
|
82
|
+
return zeroValue.number === value;
|
|
83
|
+
}
|
|
84
|
+
case "scalar": {
|
|
85
|
+
return scalarZeroValue(field.scalar, field.longType) === value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
package/example/tsconfig.json
CHANGED
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.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"
|
|
@@ -49,18 +49,19 @@
|
|
|
49
49
|
"scripts": {
|
|
50
50
|
"clean": "rimraf ./dist",
|
|
51
51
|
"build": "npm run clean && tsc --project tsconfig.json --outDir ./dist",
|
|
52
|
-
"release:version": "npm version patch -m \"release: v%s\" --no-git-tag-version",
|
|
53
|
-
"release:version:minor": "npm version minor -m \"release: v%s\" --no-git-tag-version",
|
|
54
|
-
"release:commit": "git reset && git add package.json && git commit -s -m \"release: v$npm_package_version\" && git tag v$npm_package_version",
|
|
55
|
-
"release:publish": "git push && git push --tags && npm run build && npm publish",
|
|
56
|
-
"release": "npm run release:version && npm run release:commit",
|
|
57
|
-
"release:minor": "npm run release:version:minor && npm run release:commit",
|
|
58
52
|
"typecheck": "tsc --noEmit --project tsconfig.json --outDir ./dist",
|
|
59
53
|
"gen": "npm run build && npm run gen:wkt && npm run gen:example",
|
|
60
54
|
"gen:wkt": "cd ./src && bash gen.bash",
|
|
61
55
|
"gen:example": "bash gen.bash ./example/example.proto",
|
|
62
56
|
"format": "prettier --write './src/**/(*.ts|*.tsx|*.html|*.css|*.scss)'",
|
|
63
|
-
"precommit": "lint-staged"
|
|
57
|
+
"precommit": "lint-staged",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"release:version": "npm version patch -m \"release: v%s\" --no-git-tag-version",
|
|
60
|
+
"release:version:minor": "npm version minor -m \"release: v%s\" --no-git-tag-version",
|
|
61
|
+
"release:commit": "git reset && git add package.json && git commit -s -m \"release: v$npm_package_version\" && git tag v$npm_package_version",
|
|
62
|
+
"release:publish": "git push && git push --tags && npm run build && npm publish",
|
|
63
|
+
"release": "npm run release:version && npm run release:commit",
|
|
64
|
+
"release:minor": "npm run release:version:minor && npm run release:commit"
|
|
64
65
|
},
|
|
65
66
|
"preferUnplugged": true,
|
|
66
67
|
"pre-commit": [
|
|
@@ -76,7 +77,8 @@
|
|
|
76
77
|
"pre-commit": "^1.2.2",
|
|
77
78
|
"prettier": "^3.2.5",
|
|
78
79
|
"rimraf": "^5.0.5",
|
|
79
|
-
"typescript": "^5.4.5"
|
|
80
|
+
"typescript": "^5.4.5",
|
|
81
|
+
"vitest": "^1.5.3"
|
|
80
82
|
},
|
|
81
83
|
"lint-staged": {
|
|
82
84
|
"package.json": "prettier --write",
|