diff --git a/packages/protobuf-bench/README.md b/packages/protobuf-bench/README.md index d25ce69a4..426da7776 100644 --- a/packages/protobuf-bench/README.md +++ b/packages/protobuf-bench/README.md @@ -10,5 +10,5 @@ server would usually do. | code generator | bundle size | minified | compressed | |---------------------|------------------------:|-----------------------:|-------------------:| -| protobuf-es | 127,064 b | 65,515 b | 15,970 b | +| protobuf-es | 127,054 b | 65,501 b | 15,983 b | | protobuf-javascript | 394,384 b | 288,654 b | 45,122 b | diff --git a/packages/protobuf-test/src/helpers.ts b/packages/protobuf-test/src/helpers.ts index 7135e4bab..97b988ead 100644 --- a/packages/protobuf-test/src/helpers.ts +++ b/packages/protobuf-test/src/helpers.ts @@ -35,11 +35,11 @@ export async function compileFileDescriptorSet( return fromBinary(FileDescriptorSetDesc, bytes); } -export async function compileFile(proto: string) { +export async function compileFile(proto: string, name = "input.proto") { upstreamProtobuf = upstreamProtobuf ?? new UpstreamProtobuf(); const bytes = await upstreamProtobuf.compileToDescriptorSet( { - "input.proto": proto, + [name]: proto, }, { includeImports: true, @@ -49,7 +49,7 @@ export async function compileFile(proto: string) { ); const fds = fromBinary(FileDescriptorSetDesc, bytes); const reg = createFileRegistry(fds); - const file = reg.getFile("input.proto"); + const file = reg.getFile(name); assert(file); return file; } diff --git a/packages/protobuf-test/src/reflect/registry.test.ts b/packages/protobuf-test/src/reflect/registry.test.ts index cfa6dce90..2ace91588 100644 --- a/packages/protobuf-test/src/reflect/registry.test.ts +++ b/packages/protobuf-test/src/reflect/registry.test.ts @@ -40,6 +40,7 @@ import { import { compileEnum, compileField, + compileFile, compileFileDescriptorSet, compileMessage, compileMethod, @@ -502,31 +503,16 @@ describe("createFileRegistry()", function () { describe("DescFile", () => { test("proto2 syntax", async () => { - const fileDescriptorSet = await compileFileDescriptorSet({ - "a.proto": `syntax="proto2";`, - }); - const reg = createFileRegistry(fileDescriptorSet); - const descFile = reg.getFile("a.proto"); - expect(descFile).toBeDefined(); - expect(descFile?.edition).toBe(Edition.EDITION_PROTO2); + const file = await compileFile(`syntax="proto2";`); + expect(file.edition).toBe(Edition.EDITION_PROTO2); }); test("proto3 syntax", async () => { - const fileDescriptorSet = await compileFileDescriptorSet({ - "a.proto": `syntax="proto3";`, - }); - const reg = createFileRegistry(fileDescriptorSet); - const descFile = reg.getFile("a.proto"); - expect(descFile).toBeDefined(); - expect(descFile?.edition).toBe(Edition.EDITION_PROTO3); + const file = await compileFile(`syntax="proto3";`); + expect(file.edition).toBe(Edition.EDITION_PROTO3); }); test("edition 2023", async () => { - const fileDescriptorSet = await compileFileDescriptorSet({ - "a.proto": `edition = "2023";`, - }); - const reg = createFileRegistry(fileDescriptorSet); - const descFile = reg.getFile("a.proto"); - expect(descFile).toBeDefined(); - expect(descFile?.edition).toBe(Edition.EDITION_2023); + const file = await compileFile(`edition = "2023";`); + expect(file.edition).toBe(Edition.EDITION_2023); }); test("dependencies", async () => { const fileDescriptorSet = await compileFileDescriptorSet({ @@ -542,6 +528,19 @@ describe("DescFile", () => { expect(a?.dependencies.length).toBe(2); expect(a?.dependencies.map((f) => f.name)).toStrictEqual(["b", "c"]); }); + describe("name", () => { + test("is proto file name without .proto suffix", async () => { + const file = await compileFile(`syntax="proto3";`, "foo/bar/baz.proto"); + expect(file.name).toBe("foo/bar/baz"); + }); + test("strips only last .proto", async () => { + const file = await compileFile( + `syntax="proto3";`, + "foo.proto/baz.proto.proto", + ); + expect(file.name).toBe("foo.proto/baz.proto"); + }); + }); }); describe("DescEnum", () => { diff --git a/packages/protobuf/src/reflect/registry.ts b/packages/protobuf/src/reflect/registry.ts index cdff2b83d..da0dae067 100644 --- a/packages/protobuf/src/reflect/registry.ts +++ b/packages/protobuf/src/reflect/registry.ts @@ -443,7 +443,7 @@ function addFile(proto: FileDescriptorProto, reg: MutableRegistry): void { proto, deprecated: proto.options?.deprecated ?? false, edition: getFileEdition(proto), - name: proto.name.replace(/\.proto/, ""), + name: proto.name.replace(/\.proto$/, ""), dependencies: findFileDependencies(proto, reg), enums: [], messages: [], @@ -451,7 +451,7 @@ function addFile(proto: FileDescriptorProto, reg: MutableRegistry): void { services: [], toString(): string { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above - return `file ${this.proto.name}`; + return `file ${proto.name}`; }, }; const mapEntriesStore = new Map(); @@ -829,7 +829,7 @@ function newField( keyField.scalar != ScalarType.FLOAT && keyField.scalar != ScalarType.DOUBLE, ); - const valueField = mapEntry.fields.find((f) => f.proto.number === 2); + const valueField = mapEntry.fields.find((f) => f.number === 2); assert(valueField); assert(valueField.fieldKind != "list" && valueField.fieldKind != "map"); field.mapKey = keyField.scalar;