9c843b79ba
- 新增 users 和 user_settings 表,repositories/analyses/review_notes 添加 user_id 列 - 实现基于邮箱+密码的注册登录,密码使用 bcrypt 哈希 - 使用 gin-contrib/sessions cookie-based session 管理 - 所有仓库、分析记录、review notes 按用户隔离 - 用户设置(LLM 配置、review 参数、缓存配置)独立存储 - 新增登录/注册页面,导航栏显示用户邮箱和退出按钮 - 前端 fetch 请求统一添加 credentials: 'same-origin' - 支持 SESSION_SECRET 环境变量配置会话密钥
92 lines
3.5 KiB
JavaScript
92 lines
3.5 KiB
JavaScript
// SSE (Server-Sent Events) client for POST-based streaming endpoints
|
|
// Works with fetch() + ReadableStream since EventSource only supports GET
|
|
const SSE = {
|
|
/**
|
|
* POST to a streaming endpoint and handle SSE events.
|
|
* @param {string} url - The endpoint URL
|
|
* @param {object} body - JSON request body
|
|
* @param {object} handlers - Map of event name → callback(data)
|
|
* Special handlers:
|
|
* 'error' - called on error events or fetch failures
|
|
* 'done' - called when stream ends
|
|
* 'start' - called when stream starts (first event)
|
|
* @returns {object} controller with abort() method
|
|
*/
|
|
async post(url, body, handlers = {}) {
|
|
const controller = new AbortController();
|
|
|
|
const run = async () => {
|
|
try {
|
|
const resp = await fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify(body),
|
|
signal: controller.signal,
|
|
});
|
|
|
|
if (!resp.ok) {
|
|
const errText = await resp.text();
|
|
let msg = `HTTP ${resp.status}`;
|
|
try {
|
|
const errJson = JSON.parse(errText);
|
|
msg = errJson.error || errJson.message || msg;
|
|
} catch (_) {
|
|
msg = errText || msg;
|
|
}
|
|
if (handlers.error) handlers.error({ message: msg });
|
|
return;
|
|
}
|
|
|
|
const reader = resp.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let buffer = '';
|
|
let currentEvent = '';
|
|
let doneReceived = false;
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
const lines = buffer.split('\n');
|
|
buffer = lines.pop() || '';
|
|
|
|
for (const line of lines) {
|
|
if (line.startsWith('event: ')) {
|
|
currentEvent = line.slice(7).trim();
|
|
} else if (line.startsWith('data: ')) {
|
|
const raw = line.slice(6);
|
|
let data;
|
|
try {
|
|
data = JSON.parse(raw);
|
|
} catch (_) {
|
|
data = raw;
|
|
}
|
|
|
|
// Track explicit done event from backend
|
|
if (currentEvent === 'done') doneReceived = true;
|
|
|
|
// Call the matching handler
|
|
if (currentEvent && handlers[currentEvent]) {
|
|
handlers[currentEvent](data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Stream ended — only fire done if backend didn't send an explicit one
|
|
if (handlers.done && !doneReceived) handlers.done();
|
|
} catch (err) {
|
|
if (err.name === 'AbortError') return;
|
|
if (handlers.error) handlers.error({ message: err.message });
|
|
}
|
|
};
|
|
|
|
run();
|
|
return { abort: () => controller.abort() };
|
|
}
|
|
};
|
|
|
|
window.SSE = SSE;
|