-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make edition feature defaults from
protoc
available
- Loading branch information
Showing
12 changed files
with
261 additions
and
119 deletions.
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`); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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.
nit (typo):
minimum supported edition...