Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't use .fromBinary on import #681

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

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

7 changes: 2 additions & 5 deletions packages/protobuf/src/private/feature-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ import {
FeatureSet,
FeatureSetDefaults,
} from "../google/protobuf/descriptor_pb.js";
import { protoBase64 } from "../proto-base64.js";

/**
* Static edition feature defaults supported by @bufbuild/protobuf.
*/
export const featureSetDefaults = FeatureSetDefaults.fromBinary(
protoBase64.dec(
/*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/,
),
export const featureSetDefaults = FeatureSetDefaults.fromJsonString(
/*upstream-inject-feature-defaults-start*/ '{"defaults":[{"edition":"EDITION_PROTO2","features":{"fieldPresence":"EXPLICIT","enumType":"CLOSED","repeatedFieldEncoding":"EXPANDED","utf8Validation":"NONE","messageEncoding":"LENGTH_PREFIXED","jsonFormat":"LEGACY_BEST_EFFORT"}},{"edition":"EDITION_PROTO3","features":{"fieldPresence":"IMPLICIT","enumType":"OPEN","repeatedFieldEncoding":"PACKED","utf8Validation":"VERIFY","messageEncoding":"LENGTH_PREFIXED","jsonFormat":"ALLOW"}},{"edition":"EDITION_2023","features":{"fieldPresence":"EXPLICIT","enumType":"OPEN","repeatedFieldEncoding":"PACKED","utf8Validation":"VERIFY","messageEncoding":"LENGTH_PREFIXED","jsonFormat":"ALLOW"}}],"minimumEdition":"EDITION_PROTO2","maximumEdition":"EDITION_2023"}' /*upstream-inject-feature-defaults-end*/,
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { argv, exit, stdout, stderr } from "node:process";
import { parseArgs } from "node:util";
import { readFileSync, writeFileSync } from "node:fs";
import { UpstreamProtobuf } from "../index.mjs";
import { FeatureSetDefaults, protoBase64 } from "@bufbuild/protobuf";

void main(argv.slice(2));

Expand Down Expand Up @@ -45,7 +46,11 @@ async function main(args) {
);
for (const path of positionals) {
const content = readFileSync(path, "utf-8");
const r = inject(content, ` "${defaults.toString("base64url")}" `);
const base64String = defaults.toString("base64url");
const buffer = protoBase64.dec(base64String);
const featureSetDefaults = FeatureSetDefaults.fromBinary(buffer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on our own runtime can be an issue here, because we need this package to bootstrap it 😓

I'm not completely sure it'll be problem in practice now or in the future, but I'm sure that not using our own runtime within this helper package avoids the problem 100% 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I did think about it, but as we only depend on some google types, it didn't seem to be an issue. However I do agree it's best to avoid it :)

const jsonString = featureSetDefaults.toJsonString();
const r = inject(content, ` '${jsonString}' `);
if (!r.ok) {
stderr.write(`Error injecting into ${path}: ${r.message}\n`, () =>
exit(1),
Expand Down
1 change: 1 addition & 0 deletions packages/upstream-protobuf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
},
"dependencies": {
"@bufbuild/protobuf": "*",
"fflate": "^0.8.1",
"micromatch": "^4.0.5"
}
Expand Down