-
Notifications
You must be signed in to change notification settings - Fork 71
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
Make edition feature defaults from protoc
available
#615
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5136ac8
Make edition feature defaults from `protoc` available
timostamm e16130a
Make edition feature defaults from `protoc` available
timostamm 610fc3b
Merge remote-tracking branch 'origin/tstamm/edition-feature-defaults'…
timostamm 9e1b4b8
Merge remote-tracking branch 'origin/main' into tstamm/edition-featur…
timostamm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# Upstream protobuf | ||
|
||
This package provides `protoc`, `conformance_test_runner`, and related proto | ||
files via npm "binaries". | ||
This package provides `protoc`, `conformance_test_runner`, related proto files, | ||
and feature-set defaults for editions via npm "binaries", and via an exported | ||
class. | ||
|
||
To update this project to use a new version, update the version number in | ||
version.txt and run `make`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/upstream-protobuf/bin/upstream-inject-feature-defaults.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/usr/bin/env node | ||
|
||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {argv, exit, stdout, stderr} from "node:process"; | ||
import {parseArgs} from "node:util"; | ||
import {readFileSync, writeFileSync} from "node:fs"; | ||
import {UpstreamProtobuf} from "../index.mjs"; | ||
|
||
void main(argv.slice(2)); | ||
|
||
async function main(args) { | ||
let min, max, positionals; | ||
try { | ||
({values: {min, max}, positionals} = parseArgs({ | ||
args, | ||
options: { | ||
min: { type: 'string' }, | ||
max: { type: 'string' }, | ||
}, | ||
allowPositionals: true, | ||
})); | ||
} catch { | ||
exitUsage(); | ||
} | ||
const upstream = new UpstreamProtobuf(); | ||
const defaults = await upstream.getFeatureSetDefaults(min, max); | ||
stdout.write(`Injecting google.protobuf.FeatureSetDefaults into ${positionals.length} files...\n`); | ||
for (const path of positionals) { | ||
const content = readFileSync(path, "utf-8"); | ||
const r = inject(content, `"${defaults.toString("base64url")}"`); | ||
if (!r.ok) { | ||
stderr.write(`Error injecting into ${path}: ${r.message}\n`); | ||
exit(1); | ||
} | ||
if (r.newContent === content) { | ||
stdout.write(`- ${path} - no changes\n`); | ||
continue; | ||
} | ||
writeFileSync(path, r.newContent); | ||
stdout.write(`- ${path} - updated\n`); | ||
} | ||
} | ||
|
||
/** | ||
* @typedef {object} InjectSuccess | ||
* @property {true} ok | ||
* @property {string} newContent | ||
*/ | ||
/** | ||
* @typedef {object} InjectError | ||
* @property {false} ok | ||
* @property {string} message | ||
*/ | ||
/** | ||
* @param {string} content | ||
* @param {string} contentToInject | ||
* @return {InjectSuccess | InjectError} | ||
*/ | ||
function inject(content, contentToInject) { | ||
const tokenStart = "/*upstream-inject-feature-defaults-start*/"; | ||
const tokenEnd = "/*upstream-inject-feature-defaults-end*/"; | ||
const indexStart = content.indexOf(tokenStart); | ||
const indexEnd = content.indexOf(tokenEnd); | ||
if (indexStart < 0 || indexEnd < 0) { | ||
return {ok: false, message: "missing comment annotations"}; | ||
} | ||
if (indexEnd < indexStart) { | ||
return {ok: false, message: "invalid comment annotations"}; | ||
} | ||
const head = content.substring(0, indexStart + tokenStart.length); | ||
const foot = content.substring(indexEnd); | ||
const newContent = head + contentToInject + foot; | ||
return {ok: true, newContent}; | ||
} | ||
|
||
/** | ||
* @return never | ||
*/ | ||
function exitUsage() { | ||
stderr.write(`USAGE: upstream-inject-feature-defaults [--min <mininum supported edition>] [--max <maximum supported edition>] <file-to-inject-into>\n`); | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
export declare class UpstreamProtobuf { | ||
constructor(temp?: string, version?: string); | ||
|
||
getProtocPath(): Promise<string>; | ||
|
||
getFeatureSetDefaults( | ||
minimumEdition?: string, | ||
maximumEdition?: string, | ||
): Promise<Uint8Array>; | ||
|
||
compileToDescriptorSet(files: Record<string, string>): Promise<Uint8Array>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're here, I've also added the
compileToDescriptorSet
method, which gives us the generic option to generate descriptors from inline protobuf source, and makes this test a bit more readable.