feat: 更新版本号和依赖项,添加获取工作空间 Cookie 的功能,新增测试用例
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user