Skip to content

Commit

Permalink
fix(Schema generation): ensure Buffer api is only used when in nodejs…
Browse files Browse the repository at this point in the history
… environment
  • Loading branch information
zak-cloudnc committed Nov 10, 2024
1 parent 38b114c commit 6dcb434
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 15 additions & 2 deletions integration/options/options.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SourceInfo from "./sourceInfo";
import { impFile, maybePrefixPackage } from "./utils";
import { basicTypeName, toReaderCall } from "./types";
import { BinaryReader } from "@bufbuild/protobuf/wire";
import { OutputSchemaOption } from "./options";
import { EnvOption, OutputSchemaOption } from "./options";

const fileDescriptorProto = imp("FileDescriptorProto@ts-proto-descriptors");

Expand Down Expand Up @@ -216,7 +216,11 @@ function getExtensionValue(ctx: Context, extension: FieldDescriptorProto, data:
}),
);
const result = resultBuffer.toString("base64");
return code`'${extension.name}': ${typeName}.decode(Buffer.from('${result}', 'base64'))`;
const encoded =
ctx.options.env === EnvOption.NODE
? code`Buffer.from(${ctx.utils.bytesFromBase64}("${result}")`
: code`${ctx.utils.bytesFromBase64}("${result}")`;
return code`'${extension.name}': ${typeName}.decode(${encoded})`;
} else {
const reader = new BinaryReader(data[0]);
let value = (reader as any)[toReaderCall(extension)]();
Expand Down

0 comments on commit 6dcb434

Please sign in to comment.