Skip to content

Commit

Permalink
Avoid instanceof Message (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Feb 20, 2024
1 parent b197818 commit 084f9d3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/connect-web-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
4 changes: 2 additions & 2 deletions packages/connect/src/connect-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 7 additions & 3 deletions packages/connect/src/http-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/protocol-connect/error-json.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 { Message, protoBase64 } from "@bufbuild/protobuf";
import { protoBase64 } from "@bufbuild/protobuf";
import type {
JsonObject,
JsonValue,
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/protocol-grpc/trailer-status.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 { 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";
Expand Down Expand Up @@ -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}`,
Expand Down

0 comments on commit 084f9d3

Please sign in to comment.