feat: 更新版本号和依赖项,添加获取工作空间 Cookie 的功能,新增测试用例

This commit is contained in:
2026-02-25 16:41:41 +08:00
parent e6042e025f
commit dd691f7a59
7 changed files with 1031 additions and 10 deletions

View File

@@ -79,7 +79,7 @@ export class CNBCore {
}
delete _headers.Authorization;
}
console.log('Request URL:', url, data, _headers);
// console.log('Request URL:', url, data, _headers);
const response = await fetch(url || '', {
method,
headers: _headers,

View File

@@ -16,6 +16,18 @@ export class User extends CNBCore {
useCookie: true,
});
}
/**
* 判断当前 Cookie 是否有效
* @returns
*/
async checkCookieValid(): Promise<Result> {
const user = await this.getCurrentUser();
if (user.code === 200) {
return { code: 200, message: 'cookie valid' };
} else {
return { code: 401, message: 'cookie invalid' };
}
}
/**
* 使用 Token 获取用户信息
* @returns

View File

@@ -113,7 +113,62 @@ export class Workspace extends CNBCore {
return this.post({ url: `/${repo}/-/workspace/start`, data });
}
/**
* 添加使用cookie获取工作空间访问权限的功能适用于需要保持工作空间连接状态的场景
* 例如使用 WebSocket 连接工作空间时需要携带 cookie 进行身份验证。
* https://cnb.cool/kevisual/dev-env/-/workspace/vscode-web/cnb-708-1ji9sog7o-001
* @param repo
* @param pipelineId
* @returns
*/
async getWorkspaceCookie(repo: string, pipelineId: string): Promise<Result<{ value: string, cookie: string; cookieName: string }>> {
const url = `${this.hackURL}/${repo}/-/workspace/vscode-web/${pipelineId}`;
const response = await fetch(url, {
method: 'GET',
redirect: 'manual',
headers: {
'Cookie': this.cookie || '',
'Accept': 'application/json',
}
});
// 第一次 302 重定向
if (response.status === 302 || response.status === 301) {
// 包含token的重定向 URL 通常在 Location 头中返回
// 类似 https://cnb-708-1ji9sog7o-001.cnb.space/login?t=orange:workspace:login-token:963691a2-35ce-4fef-a7ba-72723cefd226
const loginURL = response.headers.get('Location');
// 从 URL 参数中获取 cookieName例如: orange:workspace:cookie-session:cnb-708-1ji9sog7o-001
const cookieName = `orange:workspace:cookie-session:${pipelineId}`;
// 第二次请求,也设置为 manual 防止自动重定向
const response2 = await fetch(loginURL || '', {
method: 'GET',
redirect: 'manual',
headers: {
'Cookie': this.cookie || '',
'Accept': 'application/json',
}
});
// 第二次 302 重定向,获取最终的 cookie 值
if (response2.status === 302 || response2.status === 301) {
// 从 Set-Cookie 头中获取 cookie 值
const setCookie = response2.headers.get('Set-Cookie');
// 解析 cookie 值
const cookieValue = setCookie?.split(';')[0]?.split('=')[1] || '';
return {
code: 200,
message: 'success',
data: { value: cookieValue, cookieName, cookie: `${cookieName}=${cookieValue}` }
};
}
// 如果不是重定向,尝试获取 JSON 数据
return { code: 500 };
}
return { code: 500, };
}
}
export interface WorkspaceLinkDetail {
codebuddy: string;