Skip to content

Commit

Permalink
V2: Move Registry to the top level exports (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored May 14, 2024
1 parent d1cd584 commit fe66d80
Show file tree
Hide file tree
Showing 42 changed files with 210 additions and 185 deletions.
2 changes: 1 addition & 1 deletion packages/protobuf-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ server would usually do.

| code generator | bundle size | minified | compressed |
|---------------------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 126,744 b | 66,286 b | 16,005 b |
| protobuf-es | 126,776 b | 66,286 b | 15,998 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
18 changes: 9 additions & 9 deletions packages/protobuf-test/src/reflect/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import {
type FileRegistry,
createFileRegistry,
createRegistry,
LongType,
protoCamelCase,
ScalarType,
} from "@bufbuild/protobuf/reflect";
import type {
DescEnum,
DescExtension,
DescFile,
DescMessage,
DescOneof,
DescService,
import {
type DescEnum,
type DescExtension,
type DescFile,
type DescMessage,
type DescOneof,
type DescService,
LongType,
ScalarType,
} from "@bufbuild/protobuf";
import {
type FileDescriptorSet,
Expand Down
3 changes: 1 addition & 2 deletions packages/protobuf/src/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
// limitations under the License.

import type { MessageShape } from "./types.js";
import type { DescField, DescMessage } from "./desc-types.js";
import { type DescField, type DescMessage, ScalarType } from "./descriptors.js";
import type { ReflectMessage } from "./reflect/reflect-types.js";
import { reflect } from "./reflect/reflect.js";
import { isReflectMessage } from "./reflect/guard.js";
import { ScalarType } from "./reflect/scalar.js";

/**
* Create a deep copy of a message, including extensions and unknown fields.
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf/src/codegenv1/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import type {
FieldOptions,
EnumDescriptorProto,
} from "../wkt/gen/google/protobuf/descriptor_pb.js";
import type { DescFile } from "../desc-types.js";
import type { DescFile } from "../descriptors.js";
import { restoreJsonNames } from "./restore-json-names.js";
import { createFileRegistry } from "../reflect/registry.js";
import { createFileRegistry } from "../registry.js";
import { assert } from "../reflect/assert.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
DescExtension,
DescMessage,
DescService,
} from "../desc-types.js";
} from "../descriptors.js";
import { protoCamelCase } from "../reflect/names.js";
import { assert } from "../reflect/assert.js";
import { isFieldSet, clearField } from "../fields.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescEnum, DescFile } from "../desc-types.js";
import type { DescEnum, DescFile } from "../descriptors.js";
import type { GenDescEnum } from "./types.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import type { Message } from "../types.js";
import type { DescFile } from "../desc-types.js";
import type { DescFile } from "../descriptors.js";
import type { GenDescExtension } from "./types.js";

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/protobuf/src/codegenv1/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import { base64Decode } from "../wire/index.js";
import { FileDescriptorProtoDesc } from "../wkt/gen/google/protobuf/descriptor_pb.js";
import type { DescFile } from "../desc-types.js";
import { createFileRegistry } from "../reflect/registry.js";
import type { DescFile } from "../descriptors.js";
import { createFileRegistry } from "../registry.js";
import { assert } from "../reflect/assert.js";
import { restoreJsonNames } from "./restore-json-names.js";
import { fromBinary } from "../from-binary.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import type { Message } from "../types.js";
import type { DescFile } from "../desc-types.js";
import type { DescFile } from "../descriptors.js";
import type { GenDescMessage } from "./types.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import type { GenDescService, GenDescServiceShape } from "./types.js";
import type { DescFile } from "../desc-types.js";
import type { DescFile } from "../descriptors.js";

/**
* Hydrate a service descriptor.
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/codegenv1/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
DescFile,
DescMessage,
DescService,
} from "../desc-types.js";
} from "../descriptors.js";

/**
* Describes a protobuf source file.
Expand Down
10 changes: 8 additions & 2 deletions packages/protobuf/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
// limitations under the License.

import { isMessage } from "./is-message.js";
import type { DescField, DescMessage, DescOneof } from "./desc-types.js";
import {
type DescField,
type DescMessage,
type DescOneof,
LongType,
ScalarType,
} from "./descriptors.js";
import type { Message, MessageInitShape, MessageShape } from "./types.js";
import { LongType, ScalarType, scalarZeroValue } from "./reflect/scalar.js";
import { scalarZeroValue } from "./reflect/scalar.js";
import { FieldError } from "./reflect/error.js";
import { isObject, type OneofADT } from "./reflect/guard.js";
import { unsafeGet, unsafeOneofCase, unsafeSet } from "./reflect/unsafe.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
OneofDescriptorProto,
ServiceDescriptorProto,
} from "./wkt/gen/google/protobuf/descriptor_pb.js";
import type { LongType, ScalarType, ScalarValue } from "./reflect/scalar.js";
import type { ScalarValue } from "./reflect/scalar.js";

export type SupportedEdition = Extract<
Edition,
Expand All @@ -39,6 +39,73 @@ type SupportedFieldPresence = Extract<
| FeatureSet_FieldPresence.LEGACY_REQUIRED
>;

/**
* Scalar value types. This is a subset of field types declared by protobuf
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
* are omitted, but the numerical values are identical.
*/
export enum ScalarType {
// 0 is reserved for errors.
// Order is weird for historical reasons.
DOUBLE = 1,
FLOAT = 2,
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
// negative values are likely.
INT64 = 3,
UINT64 = 4,
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
// negative values are likely.
INT32 = 5,
FIXED64 = 6,
FIXED32 = 7,
BOOL = 8,
STRING = 9,
// Tag-delimited aggregate.
// Group type is deprecated and not supported in proto3. However, Proto3
// implementations should still be able to parse the group wire format and
// treat group fields as unknown fields.
// TYPE_GROUP = 10,
// TYPE_MESSAGE = 11, // Length-delimited aggregate.

// New in version 2.
BYTES = 12,
UINT32 = 13,
// TYPE_ENUM = 14,
SFIXED32 = 15,
SFIXED64 = 16,
SINT32 = 17, // Uses ZigZag encoding.
SINT64 = 18, // Uses ZigZag encoding.
}

/**
* JavaScript representation of fields with 64 bit integral types (int64, uint64,
* sint64, fixed64, sfixed64).
*
* This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
* JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
* String if `[jstype = JS_STRING]` is specified.
*
* ```protobuf
* uint64 field_a = 1; // BigInt
* uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
* uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
* uint64 field_b = 2 [jstype = JS_STRING]; // String
* ```
*/
export enum LongType {
/**
* Use JavaScript BigInt.
*/
BIGINT = 0,

/**
* Use JavaScript String.
*
* Field option `[jstype = JS_STRING]`.
*/
STRING = 1,
}

/**
* A union of all descriptors, discriminated by a `kind` property.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import type { MessageShape } from "./types.js";
import { scalarEquals } from "./reflect/scalar.js";
import { reflect } from "./reflect/reflect.js";
import type { DescField, DescMessage } from "./desc-types.js";
import type { DescField, DescMessage } from "./descriptors.js";
import type { MapEntryKey, ReflectMessage } from "./reflect/index.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
DescMethod,
DescOneof,
DescService,
} from "./desc-types.js";
} from "./descriptors.js";
import { assert } from "./reflect/assert.js";
import { create } from "./create.js";
import { readField } from "./from-binary.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import type { MessageShape } from "./types.js";
import type { DescField, DescMessage } from "./desc-types.js";
import type { DescField, DescMessage } from "./descriptors.js";
import { unsafeClear, unsafeIsSet } from "./reflect/unsafe.js";

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/protobuf/src/from-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescField, DescMessage } from "./desc-types.js";
import {
type DescField,
type DescMessage,
LongType,
ScalarType,
} from "./descriptors.js";
import type { MessageShape } from "./types.js";
import type { MapEntryKey, ReflectMessage } from "./reflect/index.js";
import { LongType, ScalarType, scalarZeroValue } from "./reflect/scalar.js";
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";
Expand Down
25 changes: 11 additions & 14 deletions packages/protobuf/src/from-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@

/* eslint-disable no-case-declarations */

import type {
DescEnum,
DescExtension,
DescField,
DescMessage,
DescOneof,
} from "./desc-types.js";
import {
type DescEnum,
type DescExtension,
type DescField,
type DescMessage,
type DescOneof,
LongType,
ScalarType,
} from "./descriptors.js";
import type { JsonValue } from "./json-value.js";
import { assertFloat32, assertInt32, assertUInt32 } from "./reflect/assert.js";
import { protoInt64 } from "./proto-int64.js";
import { create } from "./create.js";
import type { Registry } from "./reflect/registry.js";
import type { Registry } from "./registry.js";
import type { MapEntryKey, ReflectMessage } from "./reflect/reflect-types.js";
import { reflect } from "./reflect/reflect.js";
import { formatVal } from "./reflect/reflect-check.js";
import {
LongType,
ScalarType,
type ScalarValue,
scalarZeroValue,
} from "./reflect/scalar.js";
import { type ScalarValue, scalarZeroValue } from "./reflect/scalar.js";
import type { MessageShape } from "./types.js";
import { base64Decode } from "./wire/base64-encoding.js";
import { getTextEncoding } from "./wire/text-encoding.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export * from "./types.js";
export * from "./is-message.js";
export * from "./create.js";
export * from "./clone.js";
export * from "./desc-types.js";
export * from "./descriptors.js";
export * from "./equals.js";
export * from "./fields.js";
export type { JsonValue, JsonObject } from "./json-value.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/is-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import type { MessageShape } from "./types.js";
import type { DescMessage } from "./desc-types.js";
import type { DescMessage } from "./descriptors.js";

/**
* Determine whether the given `arg` is a message.
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/reflect/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescField, DescOneof } from "../desc-types.js";
import type { DescField, DescOneof } from "../descriptors.js";

const errorNames = [
"FieldValueInvalidError",
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/reflect/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type {
ReflectMessage,
} from "./reflect-types.js";
import { unsafeLocal } from "./unsafe.js";
import type { DescField, DescMessage } from "../desc-types.js";
import type { DescField, DescMessage } from "../descriptors.js";
import { isMessage } from "../is-message.js";

export function isObject(arg: unknown): arg is Record<string, unknown> {
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/reflect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export * from "./names.js";
export * from "./nested-types.js";
export * from "./reflect.js";
export * from "./reflect-types.js";
export * from "./registry.js";
export * from "../registry.js";
export * from "./scalar.js";
export { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
2 changes: 1 addition & 1 deletion packages/protobuf/src/reflect/nested-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
DescFile,
DescMessage,
DescService,
} from "../desc-types.js";
} from "../descriptors.js";

/**
* Iterate over all types - enumerations, extensions, services, messages -
Expand Down
9 changes: 7 additions & 2 deletions packages/protobuf/src/reflect/reflect-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { ScalarType, scalarTypeDescription } from "./scalar.js";
import { scalarTypeDescription } from "./scalar.js";
import type { InvalidScalarValueErr } from "./scalar.js";
import { checkScalarValue } from "./scalar.js";
import type { DescEnum, DescField, DescMessage } from "../desc-types.js";
import {
type DescEnum,
type DescField,
type DescMessage,
ScalarType,
} from "../descriptors.js";
import { isMessage } from "../is-message.js";
import { FieldError } from "./error.js";
import { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
Expand Down
9 changes: 7 additions & 2 deletions packages/protobuf/src/reflect/reflect-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { DescField, DescMessage, DescOneof } from "../desc-types.js";
import {
type DescField,
type DescMessage,
type DescOneof,
LongType,
} from "../descriptors.js";
import { FieldError } from "./error.js";
import { unsafeLocal } from "./unsafe.js";
import type { Message, UnknownField } from "../types.js";
import type { LongType, ScalarValue } from "./scalar.js";
import type { ScalarValue } from "./scalar.js";

/**
* ReflectMessage provides dynamic access and manipulation of a message.
Expand Down
Loading

0 comments on commit fe66d80

Please sign in to comment.