From f9d3553d49ea98e7acd31f95fa5b3d048faabb89 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Tue, 30 Apr 2024 16:37:03 +0200 Subject: [PATCH] Rely on ReflectMessage for jstype=JS_STRING ReflectMessage converts bigint to string for us if necessary, so we do not have to support it in fromBinary and fromJson. --- packages/protobuf-bench/README.md | 2 +- packages/protobuf/src/from-binary.ts | 25 ++++--------- packages/protobuf/src/from-json.ts | 53 +++++++++++----------------- 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/packages/protobuf-bench/README.md b/packages/protobuf-bench/README.md index 3ef4ad7b5..9872fcfef 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 | 126,937 b | 65,433 b | 15,946 b | +| protobuf-es | 126,284 b | 65,142 b | 15,854 b | | protobuf-javascript | 394,384 b | 288,654 b | 45,122 b | diff --git a/packages/protobuf/src/from-binary.ts b/packages/protobuf/src/from-binary.ts index d0371c7dc..812794ec7 100644 --- a/packages/protobuf/src/from-binary.ts +++ b/packages/protobuf/src/from-binary.ts @@ -143,10 +143,10 @@ export function readField( ) { switch (field.fieldKind) { case "scalar": - message.set(field, readScalar(reader, field.scalar, field.longType)); + message.set(field, readScalar(reader, field.scalar)); break; case "enum": - message.set(field, readScalar(reader, ScalarType.INT32)); + message.set(field, readScalar(reader, ScalarType.INT32) as number); break; case "message": message.set( @@ -178,7 +178,7 @@ function readMapEntry( const [fieldNo] = reader.tag(); switch (fieldNo) { case 1: - key = readScalar(reader, field.mapKey); + key = readScalar(reader, field.mapKey) as MapEntryKey; break; case 2: switch (field.mapKind) { @@ -229,18 +229,17 @@ function readListField( return; } const scalarType = field.scalar ?? ScalarType.INT32; - const longType = field.listKind == "scalar" ? field.longType : undefined; const packed = wireType == WireType.LengthDelimited && scalarType != ScalarType.STRING && scalarType != ScalarType.BYTES; if (!packed) { - message.addListItem(field, readScalar(reader, scalarType, longType)); + message.addListItem(field, readScalar(reader, scalarType)); return; } const e = reader.uint32() + reader.pos; while (reader.pos < e) { - message.addListItem(field, readScalar(reader, scalarType, longType)); + message.addListItem(field, readScalar(reader, scalarType)); } } @@ -262,19 +261,7 @@ function readMessageField( return message; } -function readScalar( - reader: BinaryReader, - type: Scalar, - longType?: L, -): ScalarValue { - let v = readScalarValue(reader, type); - if (longType === LongType.STRING) { - v = typeof v == "bigint" ? v.toString() : v; - } - return v as ScalarValue; -} - -function readScalarValue(reader: BinaryReader, type: ScalarType): ScalarValue { +function readScalar(reader: BinaryReader, type: ScalarType): ScalarValue { switch (type) { case ScalarType.STRING: return reader.string(); diff --git a/packages/protobuf/src/from-json.ts b/packages/protobuf/src/from-json.ts index d36156d58..430f425fe 100644 --- a/packages/protobuf/src/from-json.ts +++ b/packages/protobuf/src/from-json.ts @@ -26,33 +26,33 @@ 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 { ReflectMessage, MapEntryKey } from "./reflect/reflect-types.js"; +import type { MapEntryKey, ReflectMessage } from "./reflect/reflect-types.js"; import { reflect } from "./reflect/reflect.js"; import { formatVal } from "./reflect/reflect-check.js"; import { - scalarZeroValue, LongType, ScalarType, 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"; -import { - ListValueDesc, - NullValue, - StructDesc, - ValueDesc, - anyPack, -} from "./wkt/index.js"; import type { Any, - Timestamp, Duration, - Struct, - Value, FieldMask, ListValue, + Struct, + Timestamp, + Value, +} from "./wkt/index.js"; +import { + anyPack, + ListValueDesc, + NullValue, + StructDesc, + ValueDesc, } from "./wkt/index.js"; import { isWrapperDesc } from "./wkt/wrappers.js"; import { createExtensionContainer, setExtension } from "./extensions.js"; @@ -293,7 +293,7 @@ function readMapField( msg.setMapEntry( field, key, - readScalar(field.scalar, jsonMapValue, LongType.BIGINT, true), + readScalar(field.scalar, jsonMapValue, true), ); } catch (e) { let m = `cannot decode map value for field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonMapValue)}`; @@ -346,10 +346,7 @@ function readListField( break; case "scalar": try { - msg.addListItem( - field, - readScalar(field.scalar, jsonItem, field.longType, true), - ); + msg.addListItem(field, readScalar(field.scalar, jsonItem, true)); } catch (e) { let m = `cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonItem)}`; if (e instanceof Error && e.message.length > 0) { @@ -397,7 +394,7 @@ function readScalarField( json: JsonValue, ) { try { - const scalarValue = readScalar(field.scalar, json, field.longType, false); + const scalarValue = readScalar(field.scalar, json, false); if (scalarValue === tokenNull) { msg.clear(field); } else { @@ -424,7 +421,7 @@ function readMapKey(type: ScalarType, json: JsonValue) { break; } } - return readScalar(type, json, LongType.BIGINT, true) as ScalarValue< + return readScalar(type, json, true) as ScalarValue< Exclude >; } @@ -482,24 +479,21 @@ const tokenNull = Symbol(); function readScalar( type: ScalarType, json: JsonValue, - longType: LongType, nullAsZeroValue: true, ): ScalarValue; function readScalar( type: ScalarType, json: JsonValue, - longType: LongType, nullAsZeroValue: false, ): ScalarValue | typeof tokenNull; function readScalar( type: ScalarType, json: JsonValue, - longType: LongType, nullAsZeroValue: boolean, ): ScalarValue | typeof tokenNull { if (json === null) { if (nullAsZeroValue) { - return scalarZeroValue(type, longType); + return scalarZeroValue(type, LongType.BIGINT); } return tokenNull; } @@ -558,15 +552,11 @@ function readScalar( case ScalarType.SFIXED64: case ScalarType.SINT64: if (typeof json != "number" && typeof json != "string") break; - const long = protoInt64.parse(json); - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - return longType ? long.toString() : long; + return protoInt64.parse(json); case ScalarType.FIXED64: case ScalarType.UINT64: if (typeof json != "number" && typeof json != "string") break; - const uLong = protoInt64.uParse(json); - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - return longType ? uLong.toString() : uLong; + return protoInt64.uParse(json); // bool: case ScalarType.BOOL: @@ -642,10 +632,7 @@ function tryWktFromJson( if (jsonValue === null) { msg.clear(valueField); } else { - msg.set( - valueField, - readScalar(valueField.scalar, jsonValue, valueField.longType, true), - ); + msg.set(valueField, readScalar(valueField.scalar, jsonValue, true)); } return true; }