From bfeb5a1812aff5459c82e0753727d2be2af248bf Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Mon, 19 Feb 2024 17:38:35 +0100 Subject: [PATCH] Avoid instanceof Message --- packages/connect-web-bench/README.md | 2 +- packages/connect/src/connect-error.ts | 4 ++-- packages/connect/src/http-headers.ts | 10 +++++++--- packages/connect/src/protocol-connect/error-json.ts | 4 ++-- packages/connect/src/protocol-grpc/trailer-status.ts | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/connect-web-bench/README.md b/packages/connect-web-bench/README.md index f6fbf18f4..8ccb57996 100644 --- a/packages/connect-web-bench/README.md +++ b/packages/connect-web-bench/README.md @@ -10,5 +10,5 @@ it like a web server would usually do. | code generator | bundle size | minified | compressed | |----------------|-------------------:|-----------------------:|---------------------:| -| connect | 122,817 b | 53,918 b | 14,494 b | +| connect | 122,811 b | 53,916 b | 14,497 b | | grpc-web | 415,212 b | 300,936 b | 53,420 b | diff --git a/packages/connect/src/connect-error.ts b/packages/connect/src/connect-error.ts index 3d71d5145..3646bfe43 100644 --- a/packages/connect/src/connect-error.ts +++ b/packages/connect/src/connect-error.ts @@ -14,12 +14,12 @@ import { Code } from "./code.js"; import type { + Message, AnyMessage, IMessageTypeRegistry, JsonValue, MessageType, } from "@bufbuild/protobuf"; -import { Message } from "@bufbuild/protobuf"; import { codeToString } from "./protocol-connect/code-string.js"; /** @@ -172,7 +172,7 @@ export class ConnectError extends Error { : typeOrRegistry; const details: AnyMessage[] = []; for (const data of this.details) { - if (data instanceof Message) { + if ("getType" in data) { if (registry.findMessage(data.getType().typeName)) { details.push(data); } diff --git a/packages/connect/src/http-headers.ts b/packages/connect/src/http-headers.ts index d4b19653b..e3a45f4eb 100644 --- a/packages/connect/src/http-headers.ts +++ b/packages/connect/src/http-headers.ts @@ -12,8 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -import type { BinaryReadOptions, MessageType } from "@bufbuild/protobuf"; -import { Message, protoBase64 } from "@bufbuild/protobuf"; +import type { + Message, + BinaryReadOptions, + MessageType, +} from "@bufbuild/protobuf"; +import { protoBase64 } from "@bufbuild/protobuf"; import { ConnectError } from "./connect-error.js"; import { Code } from "./code.js"; @@ -30,7 +34,7 @@ export function encodeBinaryHeader( value: Uint8Array | ArrayBufferLike | Message | string, ): string { let bytes: Uint8Array; - if (value instanceof Message) { + if (typeof value == "object" && "getType" in value) { bytes = value.toBinary(); } else if (typeof value == "string") { bytes = new TextEncoder().encode(value); diff --git a/packages/connect/src/protocol-connect/error-json.ts b/packages/connect/src/protocol-connect/error-json.ts index 67466b035..e874ca8a1 100644 --- a/packages/connect/src/protocol-connect/error-json.ts +++ b/packages/connect/src/protocol-connect/error-json.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Message, protoBase64 } from "@bufbuild/protobuf"; +import { protoBase64 } from "@bufbuild/protobuf"; import type { JsonObject, JsonValue, @@ -132,7 +132,7 @@ export function errorToJson( }; o.details = error.details .map((value) => { - if (value instanceof Message) { + if ("getType" in value) { const i: IncomingDetail = { type: value.getType().typeName, value: value.toBinary(), diff --git a/packages/connect/src/protocol-grpc/trailer-status.ts b/packages/connect/src/protocol-grpc/trailer-status.ts index 12f389d26..7708a2bd3 100644 --- a/packages/connect/src/protocol-grpc/trailer-status.ts +++ b/packages/connect/src/protocol-grpc/trailer-status.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Any, Message } from "@bufbuild/protobuf"; +import { Any } from "@bufbuild/protobuf"; import { Status } from "./gen/status_pb.js"; import { ConnectError } from "../connect-error.js"; import { decodeBinaryHeader, encodeBinaryHeader } from "../http-headers.js"; @@ -52,7 +52,7 @@ export function setTrailerStatus( code: error.code, message: error.rawMessage, details: error.details.map((value) => - value instanceof Message + "getType" in value ? Any.pack(value) : new Any({ typeUrl: `type.googleapis.com/${value.type}`,