Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/bundle-size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ usually do. We repeat this for an increasing number of files.

| code generator | files | bundle size | minified | compressed |
| ------------------- | ----: | ----------: | --------: | ---------: |
| Protobuf-ES | 1 | 131,530 b | 68,233 b | 15,681 b |
| Protobuf-ES | 4 | 133,719 b | 69,743 b | 16,342 b |
| Protobuf-ES | 8 | 136,481 b | 71,514 b | 16,880 b |
| Protobuf-ES | 16 | 146,931 b | 79,495 b | 19,248 b |
| Protobuf-ES | 32 | 174,722 b | 101,511 b | 24,714 b |
| Protobuf-ES | 1 | 132,069 b | 68,479 b | 15,746 b |
| Protobuf-ES | 4 | 134,258 b | 69,989 b | 16,445 b |
| Protobuf-ES | 8 | 137,020 b | 71,760 b | 16,938 b |
| Protobuf-ES | 16 | 147,470 b | 79,741 b | 19,274 b |
| Protobuf-ES | 32 | 175,261 b | 101,759 b | 24,722 b |
| protobuf-javascript | 1 | 104,048 b | 70,320 b | 15,540 b |
| protobuf-javascript | 4 | 130,537 b | 85,672 b | 16,956 b |
| protobuf-javascript | 8 | 152,429 b | 98,044 b | 18,138 b |
Expand Down
12 changes: 6 additions & 6 deletions packages/bundle-size/chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions packages/protobuf-test/src/enum-open-closed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { describe, expect, test } from "@jest/globals";
import { BinaryWriter, WireType } from "@bufbuild/protobuf/wire";
import { fromBinary, isFieldSet } from "@bufbuild/protobuf";
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";

describe("open enum", () => {
test("from binary sets foreign value", () => {
expect(proto3_ts.Proto3EnumSchema.open).toBe(true);
const foreignValue = 4;
expect(proto3_ts.Proto3Enum[foreignValue]).toBeUndefined();
const bytes = new BinaryWriter()
.tag(
proto3_ts.Proto3MessageSchema.field.singularEnumField.number,
WireType.Varint,
)
.int32(foreignValue)
.finish();
const msg = fromBinary(proto3_ts.Proto3MessageSchema, bytes);
const set = isFieldSet(
msg,
proto3_ts.Proto3MessageSchema.field.singularEnumField,
);
expect(set).toBe(true);
expect(msg.singularEnumField).toBe(foreignValue);
expect(msg.$unknown).toBeUndefined();
});
});

describe("closed enum", () => {
test("from binary sets foreign value as unknown field", () => {
expect(proto2_ts.Proto2EnumSchema.open).toBe(false);
const foreignValue = 4;
expect(proto2_ts.Proto2Enum[foreignValue]).toBeUndefined();
const bytes = new BinaryWriter()
.tag(
proto2_ts.Proto2MessageSchema.field.optionalEnumField.number,
WireType.Varint,
)
.int32(foreignValue)
.finish();
const msg = fromBinary(proto2_ts.Proto2MessageSchema, bytes);
const set = isFieldSet(
msg,
proto2_ts.Proto2MessageSchema.field.optionalEnumField,
);
expect(set).toBe(false);
expect(msg.optionalEnumField).toBe(proto2_ts.Proto2Enum.YES);
expect(msg.$unknown).toBeDefined();
expect(msg.$unknown?.length).toBe(1);
expect(msg.$unknown?.[0].no).toBe(
proto2_ts.Proto2MessageSchema.field.optionalEnumField.number,
);
expect(msg.$unknown?.[0].wireType).toBe(WireType.Varint);
expect(msg.$unknown?.[0].data).toStrictEqual(
new BinaryWriter().int32(foreignValue).finish(),
);
});
});
21 changes: 19 additions & 2 deletions packages/protobuf/src/from-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import type {
import { scalarZeroValue } from "./reflect/scalar.js";
import type { ScalarValue } from "./reflect/scalar.js";
import { reflect } from "./reflect/reflect.js";
import { BinaryReader, WireType } from "./wire/binary-encoding.js";
import {
BinaryReader,
BinaryWriter,
WireType,
} from "./wire/binary-encoding.js";

/**
* Options for parsing binary data.
Expand Down Expand Up @@ -151,7 +155,20 @@ export function readField(
message.set(field, readScalar(reader, field.scalar));
break;
case "enum":
message.set(field, readScalar(reader, ScalarType.INT32) as number);
const val = readScalar(reader, ScalarType.INT32);
if (field.enum.open) {
message.set(field, val);
} else {
const ok = field.enum.values.some((v) => v.number === val);
if (ok) {
message.set(field, val);
} else if (options.readUnknownFields) {
const data = new BinaryWriter().int32(val as number).finish();
const unknownFields = message.getUnknown() ?? [];
unknownFields.push({ no: field.number, wireType, data });
message.setUnknown(unknownFields);
}
}
break;
case "message":
message.set(
Expand Down