@cloudbase/toolbox 0.7.17-beta.0 → 0.7.17-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@ export interface AuthSupervisorOptions {
22
22
  */
23
23
  throwError?: boolean;
24
24
  }
25
- export type AuthFlowType = 'default' | 'web' | 'device';
25
+ export type AuthFlow = 'web' | 'device';
26
26
  export interface WebAuthOptions {
27
27
  /**
28
28
  * 请在初始化实例时指定
@@ -31,12 +31,11 @@ export interface WebAuthOptions {
31
31
  throwError?: boolean;
32
32
  /**
33
33
  * 授权方式
34
- * - 'default': 自动选择,优先 web,浏览器打开失败或无浏览器环境时降级到 device
35
- * - 'web': 强制使用网页回调授权
36
- * - 'device': 强制使用 Device Flow 授权
37
- * @default 'default'
34
+ * - 'web': 使用网页回调授权
35
+ * - 'device': 使用 Device Flow 授权
36
+ * @default 'device'
38
37
  */
39
- mode?: AuthFlowType;
38
+ flow?: AuthFlow;
40
39
  getAuthUrl?: (rawUrl: string) => string;
41
40
  noBrowser?: boolean;
42
41
  callbackTimeout?: number;
package/lib/auth/index.js CHANGED
@@ -28,7 +28,6 @@ const web_auth_1 = require("./web-auth");
28
28
  const oauth_1 = require("./oauth");
29
29
  const common_1 = require("./common");
30
30
  const credential_1 = require("./credential");
31
- const web_1 = require("../web");
32
31
  const error_1 = require("../error");
33
32
  __exportStar(require("./common"), exports);
34
33
  __exportStar(require("./credential"), exports);
@@ -83,7 +82,7 @@ class AuthSupervisor {
83
82
  */
84
83
  loginByWebAuth(options = {}) {
85
84
  return __awaiter(this, void 0, void 0, function* () {
86
- const { getAuthUrl, throwError, noBrowser, callbackTimeout, mode: authFlowType, client_id, onDeviceCode } = options;
85
+ const { getAuthUrl, throwError, noBrowser, callbackTimeout, flow = 'device', client_id, onDeviceCode } = options;
87
86
  if (this.cacheCredential && this.needCache && !this.isCacheExpire()) {
88
87
  return this.cacheCredential;
89
88
  }
@@ -92,42 +91,15 @@ class AuthSupervisor {
92
91
  if (credential)
93
92
  return credential;
94
93
  // 根据授权方式获取凭证
95
- switch (authFlowType) {
96
- case 'device':
97
- credential = yield (0, oauth_1.getAuthTokenByDeviceFlow)({ client_id, onDeviceCode });
98
- break;
99
- case 'web':
100
- credential = yield (0, web_auth_1.getAuthTokenFromWeb)({
101
- getAuthUrl,
102
- noBrowser,
103
- callbackTimeout
104
- });
105
- break;
106
- case 'default':
107
- default: {
108
- const isNoBrowser = noBrowser || (0, web_1.isTruthyFlag)(process.env.TCB_NO_BROWSER);
109
- if (isNoBrowser) {
110
- credential = yield (0, oauth_1.getAuthTokenByDeviceFlow)({ client_id, onDeviceCode });
111
- }
112
- else {
113
- try {
114
- credential = yield (0, web_auth_1.getAuthTokenFromWeb)({
115
- getAuthUrl,
116
- callbackTimeout,
117
- onOpenFailed: () => { }
118
- });
119
- }
120
- catch (e) {
121
- if ((e === null || e === void 0 ? void 0 : e.code) === 'BROWSER_OPEN_FAILED') {
122
- credential = yield (0, oauth_1.getAuthTokenByDeviceFlow)({ client_id, onDeviceCode });
123
- }
124
- else {
125
- throw e;
126
- }
127
- }
128
- }
129
- break;
130
- }
94
+ if (flow === 'web') {
95
+ credential = yield (0, web_auth_1.getAuthTokenFromWeb)({
96
+ getAuthUrl,
97
+ noBrowser,
98
+ callbackTimeout
99
+ });
100
+ }
101
+ else {
102
+ credential = yield (0, oauth_1.getAuthTokenByDeviceFlow)({ client_id, onDeviceCode });
131
103
  }
132
104
  credential = (0, common_1.resolveCredential)(credential);
133
105
  try {
@@ -4,5 +4,4 @@ export declare function getAuthTokenFromWeb(options?: {
4
4
  getAuthUrl?: (rawUrl: string) => string;
5
5
  noBrowser?: boolean;
6
6
  callbackTimeout?: number;
7
- onOpenFailed?: () => void;
8
7
  }): Promise<Credential>;
@@ -18,7 +18,7 @@ exports.CLI_AUTH_BASE_URL = 'https://tcb.cloud.tencent.com/dev';
18
18
  // 打开云开发控制台,获取授权
19
19
  function getAuthTokenFromWeb(options = {}) {
20
20
  return __awaiter(this, void 0, void 0, function* () {
21
- const { getAuthUrl, noBrowser, callbackTimeout, onOpenFailed } = options;
21
+ const { getAuthUrl, noBrowser, callbackTimeout } = options;
22
22
  const mac = yield (0, system_1.getMacAddress)();
23
23
  const os = (0, system_1.getOSInfo)();
24
24
  const macHash = (0, coding_1.md5Encoding)(mac);
@@ -44,8 +44,7 @@ function getAuthTokenFromWeb(options = {}) {
44
44
  return cliAuthUrl;
45
45
  }, 'login', {
46
46
  noBrowser,
47
- callbackTimeout,
48
- onOpenFailed
47
+ callbackTimeout
49
48
  });
50
49
  const credential = (0, common_1.resolveCredential)(query);
51
50
  return credential;
package/lib/web/web.d.ts CHANGED
@@ -6,11 +6,7 @@ export type CheckFn = (query: IQuery) => Promise<void>;
6
6
  export interface GetDataFromWebOptions {
7
7
  noBrowser?: boolean;
8
8
  callbackTimeout?: number;
9
- /** 浏览器打开失败时的回调,若提供则中止等待并抛出错误 */
10
- onOpenFailed?: () => void;
11
9
  }
12
10
  export declare function isTruthyFlag(value?: string): boolean;
13
- export declare function openUrl(url: string, options?: {
14
- skipBrowserFallback?: boolean;
15
- }): Promise<boolean>;
11
+ export declare function openUrl(url: string): Promise<boolean>;
16
12
  export declare function getDataFromWeb<T extends IQuery>(getUrl: GetUrlFn, type: 'login' | 'getData', options?: GetDataFromWebOptions): Promise<T>;
package/lib/web/web.js CHANGED
@@ -94,12 +94,11 @@ function openUrlByBrowserEnv(url) {
94
94
  function shouldUseBrowserEnvFallback() {
95
95
  return process.platform === 'linux' && isVSCodeEnvironment();
96
96
  }
97
- function openUrl(url, options = {}) {
97
+ function openUrl(url) {
98
98
  return __awaiter(this, void 0, void 0, function* () {
99
- const { skipBrowserFallback = false } = options;
100
99
  try {
101
100
  const child = yield (0, open_1.default)(url, { url: true });
102
- if ((child === null || child === void 0 ? void 0 : child.once) && !skipBrowserFallback) {
101
+ if (child === null || child === void 0 ? void 0 : child.once) {
103
102
  child.once('error', (error) => __awaiter(this, void 0, void 0, function* () {
104
103
  if (shouldUseBrowserEnvFallback()) {
105
104
  yield openUrlByBrowserEnv(url);
@@ -109,7 +108,7 @@ function openUrl(url, options = {}) {
109
108
  return true;
110
109
  }
111
110
  catch (e) {
112
- if (!skipBrowserFallback && shouldUseBrowserEnvFallback()) {
111
+ if (shouldUseBrowserEnvFallback()) {
113
112
  return openUrlByBrowserEnv(url);
114
113
  }
115
114
  return false;
@@ -134,13 +133,7 @@ function getDataFromWeb(getUrl, type, options = {}) {
134
133
  // 对 url 转码, 避免 wsl 无法正常打开地址
135
134
  // https://www.npmjs.com/package/open#url
136
135
  // https://github.com/sindresorhus/open/blob/master/index.js#L48
137
- const skipBrowserFallback = !!options.onOpenFailed;
138
- const opened = yield openUrl(url, { skipBrowserFallback });
139
- if (!opened && options.onOpenFailed) {
140
- server.close();
141
- options.onOpenFailed();
142
- throw new error_1.CloudBaseError('浏览器打开失败', { code: 'BROWSER_OPEN_FAILED' });
143
- }
136
+ yield openUrl(url);
144
137
  }
145
138
  return new Promise((resolve, reject) => {
146
139
  let finished = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/toolbox",
3
- "version": "0.7.17-beta.0",
3
+ "version": "0.7.17-beta.2",
4
4
  "description": "The toolbox for cloudbase",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {