- 后端
【免费下载链接】google-api-nodejs-client
Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
Service Control API 客户端库是 Google 服务基础设施(Service Infrastructure)的准入控制与遥测上报入口,本文以仓库内 src/apis/servicecontrol/CHANGELOG.md 为骨架,系统梳理该模块从 1.0.0 到 19.1.0 的版本演进脉络、破坏性变更来源、Node.js 运行环境要求变化与依赖升级历史,并结合 v1.ts / v2.ts 源码给出能力边界与升级实践建议。读完本文,你将能判断自己使用的版本处于哪条演进线、升级时需要注意哪些 BREAKING CHANGES,并掌握基于@googleapis/servicecontrol包的安装与调用方式。
一、Service Control API 与它的 Node.js 客户端库
Service Control API 的核心职责,按 README.md 与 v1.ts 中的原始描述是:
Provides admission control and telemetry reporting for services integrated with Service Infrastructure.
即为接入 Google 服务基础设施(Service Infrastructure)的服务提供两类能力:
- 准入控制(Admission Control):在执行操作之前判断该操作是否被允许,例如配额是否充足、策略是否通过;
- 遥测报告(Telemetry Reporting):将服务运行产生的操作日志、指标等遥测数据上报给基础设施,用于计费、审计与监控。
该模块在 monorepo 中位于 src/apis/servicecontrol/,同时以独立 npm 包@googleapis/servicecontrol形式发布(见 package.json)。其类型定义被顶层入口 src/googleapis.ts 聚合,开发者可通过google.servicecontrol('v1')或google.servicecontrol('v2')直接使用。
二、版本演进总览:从 1.0.0 到 19.1.0
CHANGELOG 记录了完整的版本线。以下按时间倒序整理全部发布记录(括号内为发布日期与变更类别):
| 版本 | 发布日期 | 变更类型 | 核心内容 |
|---|---|---|---|
| 19.1.0 | 2026-10-14 | Features | 更新 API(提交 c24c703) |
| 19.0.0 | 2026-10-03 | ⚠ BREAKING + Features | 更新 API(提交 7db141e),存在破坏性变更 |
| 18.0.1 | 2026-09-23 | Bug Fixes | 更新 API(提交 f22c782) |
| 18.0.0 | 2026-09-14 | ⚠ BREAKING + Features | 所有包重新要求最低 Node 22(issue #4020) |
| 17.0.0 | 2026-09-10 | ⚠ BREAKING + Features + Bug Fixes | 最低 Node 22(#4011);运行生成器;重新生成 index 文件;补回 Node 18 支持 |
| 16.0.0 | 2026-09-10 | ⚠ BREAKING | 最低 Node 22(#4011);补回 Node 18 支持 |
| 15.0.0 | 2026-09-09 | ⚠ BREAKING + Features | 最低 Node 22(#4011);运行生成器 |
| 14.1.1 | 2026-09-09 | Bug Fixes | 补回 Node 18 支持 |
| 14.1.0 | 2026-09-02 | Features + Bug Fixes | 运行生成器(#3992);生成包依赖升级到 Node 22 |
| 14.0.0 | 2026-08-31 | ⚠ BREAKING + Features | 运行生成器(#3919/#3955);更新 API |
| 13.1.0 | 2026-08-28 | Features | 更新 API(提交 82f4843) |
| 13.0.0 | 2026-08-03 | ⚠ BREAKING + Features | 运行生成器(#3955) |
| 12.0.0 | 2026-05-19 | ⚠ BREAKING + Features | 运行生成器(#3919);更新 API(提交 45054a1) |
| 11.2.0 | 2026-01-28 | Features | 更新 API(提交 e8e57de) |
| 11.1.0 | 2025-12-15 | Features | 更新 API(提交 f835f12、b7b21a1) |
| 11.0.1 | 2025-12-05 | Bug Fixes | 更新 API(提交 00576f0) |
| 11.0.0 | 2025-10-15 | ⚠ BREAKING + Features | 运行生成器(#3814) |
| 10.0.0 | 2025-09-30 | ⚠ BREAKING + Features | 运行生成器(#3795) |
| 9.0.0 | 2025-09-16 | ⚠ BREAKING + Bug Fixes | 为库发布做准备(#3769);googleapis-common 升级到 8.0.0(#3731) |
| 1.0.1 | 2025-08-13 | Bug Fixes | 更新 API(提交 1477ede);googleapis-common 升级到 8.0.0 |
| 1.0.0 | 2025-08-07 | Bug Fixes | 初始版本线:更新 API;googleapis-common 8.0.0 |
| 8.0.1 | 2025-07-08 | Bug Fixes | 更新 API |
| 8.0.0 | 2025-06-30 | ⚠ BREAKING + Features + Bug Fixes | 升级到 Node 18;googleapis-common 8.0.2-rc;更新 API |
| 7.0.1 | 2025-06-04 | Bug Fixes | googleapis-common 升级到 8.0.2-rc |
| 7.0.0 | 2025-05-29 | ⚠ BREAKING + Features | 升级到 Node 18 |
| 6.0.1 | 2025-05-05 | Bug Fixes | 更新 API |
| 6.0.0 | 2025-03-18 | ⚠ BREAKING + Features | 更新 API(提交 cd71b24、63c47c4) |
| 5.1.0 | 2025-03-18 | Features | 更新 API(提交 cd71b24) |
| 5.0.0 | 2025-02-04 | ⚠ BREAKING + Features | 更新 API(提交 63c47c4) |
| 4.2.0 | 2024-08-16 | Features + Bug Fixes | 更新 API;Bug Fix 更新 API |
| 4.1.1 | 2024-07-26 | Bug Fixes | 更新 API |
| 4.1.0 | 2024-05-07 | Features | 更新 API(提交 a366d52) |
| 4.0.0 | 2024-05-02 | ⚠ BREAKING + Features | 运行生成器(#3441);更新 API |
| 3.1.0 | 2024-05-02 | Features | 更新 API(提交 763243a) |
| 3.0.0 | 2024-04-04 | ⚠ BREAKING + Features | 运行生成器(#3441) |
| 2.0.0 | 2024-03-12 | ⚠ BREAKING + Features | 运行生成器(#3441) |
| 1.2.0 | 2023-10-11 | Features + Bug Fixes | 更新 API;googleapis-common 升级到 v7;更新 API(提交 ceadea7) |
| 1.1.1 | 2023-08-25 | Bug Fixes | googleapis-common 升级到 v7;更新 API |
| 1.1.0 | 2023-08-17 | Features + Bug Fixes | 更新 API |
| 1.0.0 | 2023-08-17 | Features + Bug Fixes | 初始版本线:更新 API |
| 1.0.0 | 2021-03-18 | ⚠ BREAKING + Features + Bug Fixes | 更新 API;auth 支持 workload identity federation(#2517);googleapis-common 升级到 v5(#2497);webpack 4→5(#2524) |
从这张表可以清晰看到两条规律:
- 版本号出现过重置:2021 年与 2023 年各有一条
1.0.0起点,2025 年 8 月又出现1.0.0/1.0.1的独立版本线,随后 9 月从 9.0.0 继续。这说明该模块曾经历多次发布体系调整(从单一包拆分为独立包@googleapis/servicecontrol等),升级时务必以 CHANGELOG 实际记录为准,不要假设版本号是连续递增的。 - 2026 年 8 月底~10 月为高频发布期:从 14.0.0 到 19.1.0 仅两个月内发布了 8 个版本,其中 5 个带 BREAKING CHANGES,主因是 Node 22 迁移与生成器重新运行。
三、破坏性变更(BREAKING CHANGES)盘点
CHANGELOG 中所有标记⚠ BREAKING CHANGES的版本及其来源如下:
| 版本 | 破坏性来源 |
|---|---|
| 19.0.0 | 更新 API 带来的服务端定义变化 |
| 18.0.0 | 重新要求最低 Node 版本为 22(issue #4020,所有包统一) |
| 17.0.0 | 最低 Node 22(#4011);运行生成器 |
| 16.0.0 | 最低 Node 22(#4011) |
| 15.0.0 | 最低 Node 22(#4011);运行生成器 |
| 14.0.0 | 运行生成器(#3919/#3955) |
| 13.0.0 | 运行生成器(#3955) |
| 12.0.0 | 运行生成器(#3919) |
| 11.0.0 | 运行生成器(#3814) |
| 10.0.0 | 运行生成器(#3795) |
| 9.0.0 | 为库发布做准备(#3769) |
| 8.0.0 | 升级到 Node 18 |
| 7.0.0 | 升级到 Node 18 |
| 6.0.0 | 更新 API 带来的服务端定义变化 |
| 5.0.0 | 更新 API 带来的服务端定义变化 |
| 4.0.0 / 3.0.0 / 2.0.0 | 运行生成器(#3441) |
| 1.0.0(2021) | 更新 API;auth 支持 workload identity federation |
归纳可见,破坏性变更主要有三类来源:
- Node.js 最低版本要求提升(Node 18 → Node 22),这是影响面最大的运行时变更;
- 重新运行代码生成器(run the generator),生成器会根据最新的 Discovery 文档重新生成 API 绑定,导致方法签名、类型定义或参数结构变化;
- 服务端 API 定义更新(update the API),即 Google 服务端接口本身发生演进。
四、Node.js 运行环境要求演变
Node 版本要求是本模块变更记录中最受关注的一条主线:
- 2025 年 5 月~6 月:7.0.0 与 8.0.0 连续两次以 BREAKING CHANGES 形式"升级到 Node 18",将最低运行版本提升至 Node 18;
- 2026 年 8 月~9 月:14.1.0 起"生成包依赖升级到 Node 22";随后 15.0.0、16.0.0、17.0.0 均以 BREAKING CHANGES 标记"将 apiary(生成管线)最低 Node 版本更新为 22"(issue #4011);
- 期间的反复:14.1.1、16.0.0、17.0.0 三个版本同时出现"add back support for node 18"(重新补回 Node 18 支持)的 Bug Fixes 记录,说明迁移过程中团队一度尝试兼顾 Node 18,最终在18.0.0(issue #4020)明确"重新要求最低 Node 22,且覆盖所有包"后尘埃落定;
- 当前状态:从 package.json 的
engines字段可见,现版本 19.1.0 要求"node": ">=22.0.0"。
对升级者的直接影响:若应用仍运行在 Node 18/20 上,升级到 14.0.0 之后的任何新版本前必须先完成运行时升级;反之,若停留在 14.1.1 及之前的版本,则可继续获得 Node 18 支持,但无法获得后续 API 更新。
五、依赖体系演进:googleapis-common
googleapis-common 是本模块的唯一运行时依赖(见 package.json 的dependencies),承载了 API 请求创建(createAPIRequest)、认证(GoogleAuth、AuthPlus)、HTTP 传输等核心能力。CHANGELOG 记录的升级轨迹为:
- v5:1.0.0(2021-03-18)经 issue #2497 引入;
- v7:1.1.1 / 1.2.0(2023-08-25)经依赖更新引入;
- 8.0.0:9.0.0 与 1.0.1(2025-08~09)经 issue #3731 引入;
- 8.0.2-rc:7.0.1 / 8.0.0(2025-06)作为预发布过渡版本;
- 当前:
^9.0.0(对应 19.x 系列)。
googleapis-common的版本跳跃往往与 Node 版本要求、底层gaxios传输层行为强相关,因此升级本模块时建议同步查阅 googleapis-common 的变更记录,避免传输层行为差异带来的兼容性问题。这一点也可以从 index.ts 得到印证:模块的入口实现完全基于googleapis-common的getAPI、AuthPlus、GoogleConfigurable等抽象构建。
六、API 同步机制:生成器与"update the API"
CHANGELOG 中大量重复出现的两条记录是理解本模块演进机制的关键:
**servicecontrol:** update the API:表示服务端 Discovery 文档发生更新后,客户端绑定被同步刷新;run the generator:表示重新运行 src/generator/ 下的代码生成器,批量再生成各 API 的 TypeScript 绑定与 index 文件(17.0.0 中还有独立的regenerate index files记录)。
从源码看,生成产物包含两套版本绑定:v1.ts(2151 行)与 v2.ts(1368 行),由 index.ts 中的VERSIONS映射注册:
export const VERSIONS = { v1: servicecontrol_v1.Servicecontrol, v2: servicecontrol_v2.Servicecontrol, };随后servicecontrol()工厂函数通过getAPI<T>('servicecontrol', versionOrOptions, VERSIONS, this)完成实例化(见 index.ts)。因此,每次"update the API"都会直接反映在 v1.ts / v2.ts 的类型定义与请求参数结构上——这正是升级时最需要关注 Diff 的地方。
七、API 能力边界:v1 与 v2 的接口面
结合源码可以确认两个版本的实际接口面:
v1(v1.ts):Resource$Services提供三个方法:
| 方法 | 说明 | 响应结构(源码注释中的示例) |
|---|---|---|
services.allocateQuota | 在操作执行前为指定消费者分配配额,要求servicemanagement.services.quota权限;客户端必须在服务端返回INTERNAL、UNKNOWN、DEADLINE_EXCEEDED、UNAVAILABLE时"fail-open",避免硬依赖配额功能(见 v1.ts) | allocateErrors、allocateInfo、operationId、quotaMetrics、serviceConfigId |
services.check | 执行准入控制,请求体含operation、requestProjectSettings、serviceConfigId、skipActivationCheck | checkErrors、checkInfo、operationId、quotaInfo、serviceConfigId、serviceRolloutId |
services.report | 上报遥测数据,请求体含operations、serviceConfigId | reportErrors、serviceConfigId、serviceRolloutId |
v2(v2.ts):Resource$Services提供两个方法:
services.check:v2 的准入控制接口,请求体为attributes、flags、resources、serviceConfigId。源码注释特别强调了两条硬性约束:策略传播存在约 60 秒延迟,调用方不能依赖最新策略变更;单次调用最多引用 1 个资源(hard limit of 1 referenced resources per call),涉及多个资源时必须多次调用(见 v2.ts);services.report:v2 的遥测上报接口,请求体含operations、serviceConfigId,响应含extensions(见 v2.ts)。
v1 与 v2 的差异主要在于请求/响应模型不同(v1 面向operation对象、v2 面向attributes+resources),这是升级或选型时必须厘清的点。
八、认证与调用示例
所有方法都需要认证。源码注释(如 v1.ts)给出的标准模式是使用google.auth.GoogleAuth,作用域支持cloud-platform与servicecontrol两者:
const {google} = require('googleapis'); const servicecontrol = google.servicecontrol('v1'); async function main() { const auth = new google.auth.GoogleAuth({ // Scopes 可传数组,也可传单个空格分隔字符串 scopes: [ 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/servicecontrol', ], }); // 获取认证客户端并绑定到后续所有调用 const authClient = await auth.getClient(); google.options({auth: authClient}); // 执行准入控制 const res = await servicecontrol.services.check({ // 服务名来自服务配置,例如 "pubsub.googleapis.com" serviceName: 'placeholder-value', requestBody: { operation: {}, serviceConfigId: 'my_serviceConfigId', }, }); console.log(res.data); } main().catch(e => { console.error(e); throw e; });同样的模式适用于services.report与services.allocateQuota,只需替换方法名与请求体结构。独立安装时使用:
npm install @googleapis/servicecontrol关于认证方式,CHANGELOG 中 2021 年 1.0.0 版本还记录了一项能力级更新:auth 层支持workload identity federation(issue #2517),它允许以外部身份(如 AWS、Azure 凭证)换取 Google 访问令牌,适用于混合云场景下的服务调用。
九、维护状态与新项目选型建议
这是阅读该模块文档时最需要留意的一点。README.md 明确声明:
- 该库处于maintenance mode(维护模式),Google 会继续为已有应用提供该库;
- 对于新应用,官方建议改用
@google-cloud/service-control,理由是后者"更快、更易用、维护更积极"。
因此选型建议是:
- 已有应用:可以继续使用
@googleapis/servicecontrol,跟随 CHANGELOG 完成 Node 22 迁移与定期 API 更新即可; - 新项目 / 重构旧应用:优先评估
@google-cloud/service-control,它面向 Service Control API 提供了更现代的调用体验(相关背景可参考 Google Cloud 的 Client Libraries 说明); - 无论哪条路线,API 底层的准入控制与遥测语义不变,本文的 v1/v2 能力边界分析仍然适用。
十、升级实践清单
结合全文,将 CHANGELOG 中的经验沉淀为升级检查清单:
- 先看 Node 版本:目标版本若 ≥ 14.1.0,则要求 Node 22(package.json 中
engines为>=22.0.0),先升级运行时再升级依赖; - 逐版本核对 BREAKING CHANGES:重点对比相邻版本间 v1.ts / v2.ts 中
Params$Resource$Services*与Schema$*类型定义的变化,确认请求体字段是否增减; - 关注生成器再生成批次:同一批
run the generator(如 #3919、#3955、#3441 等)会同时影响多个 API 模块,若同时使用其他 Google API,建议一起升级; - 同步升级 googleapis-common:依赖线为 v5 → v7 → 8.0.0 → 8.0.2-rc → ^9.0.0,传输层行为变化可能在升级后体现为错误信息或重试策略差异;
- 区分 v1/v2 调用模型:
check在 v1 与 v2 中请求体结构完全不同(operationvsattributes/resources),切换版本号(google.servicecontrol('v1')→('v2'))不是透明的,必须改写请求构造逻辑; - 留意 v2 的 60 秒策略传播延迟与单资源硬限制(见 v2.ts),多资源场景需多次调用。
综上,servicecontrol 模块的 CHANGELOG 不仅是一份发布流水账,更完整记录了 Google 生成式客户端库的典型生命周期:版本号重置、Node 大版本迁移、生成器驱动 API 同步、依赖线跃迁与维护模式切换。理解这条演进线,是在 google-api-nodejs-client 生态中正确升级、规避破坏性变更的前提。
- 后端
【免费下载链接】google-api-nodejs-client
Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
相关推荐
google-api-nodejs-client 中 resourcesettings 模块的版本演进与升级指南
google api nodejs client 中 resourcesettings 模块的版本演进与升级指南 src/apis/resourcesettin
后端google-api-nodejs-client 的 language 包:从 1.0 到 15.0 的版本演进、破坏性变更与升级指南
google api nodejs client 的 language 包:从 1.0 到 15.0 的版本演进、破坏性变更与升级指南 本篇文章以仓库内 src
后端google-api-nodejs-client 中 @googleapis/advisorynotifications 包的版本演进与升级指南:从 1.0.0 到 10.0.1
google api nodejs client 中 @googleapis/advisorynotifications 包的版本演进与升级指南:从 1.0.0
后端
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考