@kevisual/cnb 0.0.28 → 0.0.29

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/cnb",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
18
18
  "license": "MIT",
19
- "packageManager": "pnpm@10.30.1",
19
+ "packageManager": "pnpm@10.30.2",
20
20
  "type": "module",
21
21
  "devDependencies": {
22
22
  "@kevisual/ai": "^0.0.24",
@@ -24,7 +24,7 @@
24
24
  "@kevisual/dts": "^0.0.4",
25
25
  "@kevisual/context": "^0.0.8",
26
26
  "@kevisual/types": "^0.0.12",
27
- "@opencode-ai/plugin": "^1.2.10",
27
+ "@opencode-ai/plugin": "^1.2.12",
28
28
  "@types/bun": "^1.3.9",
29
29
  "@types/node": "^25.3.0",
30
30
  "@types/ws": "^8.18.1",
@@ -38,8 +38,8 @@
38
38
  "zod": "^4.3.6"
39
39
  },
40
40
  "dependencies": {
41
- "@kevisual/query": "^0.0.49",
42
- "@kevisual/router": "^0.0.80",
41
+ "@kevisual/query": "^0.0.52",
42
+ "@kevisual/router": "^0.0.84",
43
43
  "@kevisual/use-config": "^1.0.30",
44
44
  "es-toolkit": "^1.44.0",
45
45
  "nanoid": "^5.1.6",
package/src/cnb-core.ts CHANGED
@@ -79,7 +79,7 @@ export class CNBCore {
79
79
  }
80
80
  delete _headers.Authorization;
81
81
  }
82
- console.log('Request URL:', url, data, _headers);
82
+ // console.log('Request URL:', url, data, _headers);
83
83
  const response = await fetch(url || '', {
84
84
  method,
85
85
  headers: _headers,
package/src/user/index.ts CHANGED
@@ -16,6 +16,18 @@ export class User extends CNBCore {
16
16
  useCookie: true,
17
17
  });
18
18
  }
19
+ /**
20
+ * 判断当前 Cookie 是否有效
21
+ * @returns
22
+ */
23
+ async checkCookieValid(): Promise<Result> {
24
+ const user = await this.getCurrentUser();
25
+ if (user.code === 200) {
26
+ return { code: 200, message: 'cookie valid' };
27
+ } else {
28
+ return { code: 401, message: 'cookie invalid' };
29
+ }
30
+ }
19
31
  /**
20
32
  * 使用 Token 获取用户信息
21
33
  * @returns
@@ -113,7 +113,62 @@ export class Workspace extends CNBCore {
113
113
 
114
114
  return this.post({ url: `/${repo}/-/workspace/start`, data });
115
115
  }
116
+ /**
117
+ * 添加使用cookie获取工作空间访问权限的功能,适用于需要保持工作空间连接状态的场景,
118
+ * 例如使用 WebSocket 连接工作空间时需要携带 cookie 进行身份验证。
119
+ * https://cnb.cool/kevisual/dev-env/-/workspace/vscode-web/cnb-708-1ji9sog7o-001
120
+ * @param repo
121
+ * @param pipelineId
122
+ * @returns
123
+ */
124
+ async getWorkspaceCookie(repo: string, pipelineId: string): Promise<Result<{ value: string, cookie: string; cookieName: string }>> {
125
+ const url = `${this.hackURL}/${repo}/-/workspace/vscode-web/${pipelineId}`;
126
+ const response = await fetch(url, {
127
+ method: 'GET',
128
+ redirect: 'manual',
129
+ headers: {
130
+ 'Cookie': this.cookie || '',
131
+ 'Accept': 'application/json',
132
+ }
133
+ });
134
+
135
+ // 第一次 302 重定向
136
+ if (response.status === 302 || response.status === 301) {
137
+ // 包含token的重定向 URL 通常在 Location 头中返回
138
+ // 类似 https://cnb-708-1ji9sog7o-001.cnb.space/login?t=orange:workspace:login-token:963691a2-35ce-4fef-a7ba-72723cefd226
139
+ const loginURL = response.headers.get('Location');
140
+ // 从 URL 参数中获取 cookieName,例如: orange:workspace:cookie-session:cnb-708-1ji9sog7o-001
141
+ const cookieName = `orange:workspace:cookie-session:${pipelineId}`;
142
+ // 第二次请求,也设置为 manual 防止自动重定向
143
+ const response2 = await fetch(loginURL || '', {
144
+ method: 'GET',
145
+ redirect: 'manual',
146
+ headers: {
147
+ 'Cookie': this.cookie || '',
148
+ 'Accept': 'application/json',
149
+ }
150
+ });
151
+
152
+ // 第二次 302 重定向,获取最终的 cookie 值
153
+ if (response2.status === 302 || response2.status === 301) {
154
+ // 从 Set-Cookie 头中获取 cookie 值
155
+ const setCookie = response2.headers.get('Set-Cookie');
156
+ // 解析 cookie 值
157
+ const cookieValue = setCookie?.split(';')[0]?.split('=')[1] || '';
116
158
 
159
+ return {
160
+ code: 200,
161
+ message: 'success',
162
+ data: { value: cookieValue, cookieName, cookie: `${cookieName}=${cookieValue}` }
163
+ };
164
+ }
165
+
166
+ // 如果不是重定向,尝试获取 JSON 数据
167
+ return { code: 500 };
168
+ }
169
+
170
+ return { code: 500, };
171
+ }
117
172
  }
118
173
  export interface WorkspaceLinkDetail {
119
174
  codebuddy: string;