Skip to content

Commit 393dcf7

Browse files
authored
Merge pull request #4686 from easyops-cn/steve/v3-sse
Steve/v3-sse
2 parents 7f9c3e6 + 8267021 commit 393dcf7

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

packages/brick-container/serve/getProxy.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,16 @@ export default function getProxy(env, getRawIndexHtml) {
116116
req.path === "/next/api/auth/login/v2" &&
117117
Array.isArray(setCookies)
118118
) {
119-
// - If the server is https, but the local is http, clear the secure cookie flags;
120-
// - Otherwise, if the local is localhost and cookieSameSiteNone is enabled (default),
119+
// - If the local is localhost and cookieSameSiteNone is enabled (default),
121120
// add the secure cookie flags;
122121
// - Otherwise, if the local is https, do nothing;
123122
// - Otherwise, clear the secure cookie flags;
124123
const strategy =
125-
env.server.startsWith("https:") && !env.https
126-
? "clear"
127-
: env.cookieSameSiteNone && env.host === "localhost"
128-
? "add"
129-
: env.https
130-
? null
131-
: "clear";
124+
env.cookieSameSiteNone && env.host === "localhost"
125+
? "add"
126+
: env.https
127+
? null
128+
: "clear";
132129
if (strategy) {
133130
// Note: it seems that now Chrome (v107) requires `SameSite=None` even for localhost.
134131
// However, `Secure` can use used with non-http for localhost.

packages/utils/src/general/createSSEStream.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,53 @@ describe("createSSEStream", () => {
139139
expect(consoleError).toHaveBeenCalledWith("open error", "Internal Error");
140140
});
141141

142+
test("open error with json error", async () => {
143+
mockFetchEventSource.mockImplementation(async (_url, options) => {
144+
const { onopen, onerror } = options;
145+
await Promise.resolve();
146+
try {
147+
await onopen?.({
148+
ok: false,
149+
statusText: "Internal Error",
150+
text: () =>
151+
Promise.resolve(
152+
JSON.stringify({
153+
error: "Something went wrong",
154+
})
155+
),
156+
} as Response);
157+
} catch (e) {
158+
await onerror?.(e);
159+
}
160+
});
161+
await expect(createSSEStream(url)).rejects.toMatchInlineSnapshot(
162+
`[Error: Something went wrong]`
163+
);
164+
expect(consoleError).toHaveBeenCalledTimes(1);
165+
expect(consoleError).toHaveBeenCalledWith("open error", "Internal Error");
166+
});
167+
168+
test("open error with non-json error", async () => {
169+
mockFetchEventSource.mockImplementation(async (_url, options) => {
170+
const { onopen, onerror } = options;
171+
await Promise.resolve();
172+
try {
173+
await onopen?.({
174+
ok: false,
175+
statusText: "Internal Error",
176+
text: () => Promise.resolve("Oops"),
177+
} as Response);
178+
} catch (e) {
179+
await onerror?.(e);
180+
}
181+
});
182+
await expect(createSSEStream(url)).rejects.toMatchInlineSnapshot(
183+
`[Error: Oops]`
184+
);
185+
expect(consoleError).toHaveBeenCalledTimes(1);
186+
expect(consoleError).toHaveBeenCalledWith("open error", "Internal Error");
187+
});
188+
142189
test("message error", async () => {
143190
mockFetchEventSource.mockImplementation(async (_url, options) => {
144191
const { onopen, onmessage } = options;

packages/utils/src/general/createSSEStream.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,24 @@ export async function createSSEStream<T = unknown>(
6262
} else {
6363
// eslint-disable-next-line no-console
6464
console.error("open error", response.statusText);
65-
throw new Error(response.statusText);
65+
let text: string;
66+
try {
67+
text = await response.text();
68+
} catch {
69+
throw new Error(response.statusText);
70+
}
71+
72+
let json: any;
73+
try {
74+
json = JSON.parse(text);
75+
} catch {
76+
// Do nothing
77+
}
78+
79+
if (typeof json?.error === "string") {
80+
throw new Error(json.error);
81+
}
82+
throw new Error(text);
6683
}
6784
},
6885
onmessage(msg) {

0 commit comments

Comments
 (0)