mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(ext/fetch): do not truncate field value in EventSource
(#22368)
Depends on #22493. Closes #22367.
This commit is contained in:
parent
bf9c57aeac
commit
5c1fa0cf9c
3 changed files with 32 additions and 3 deletions
|
@ -15,7 +15,6 @@ const {
|
||||||
StringPrototypeIncludes,
|
StringPrototypeIncludes,
|
||||||
StringPrototypeIndexOf,
|
StringPrototypeIndexOf,
|
||||||
StringPrototypeSlice,
|
StringPrototypeSlice,
|
||||||
StringPrototypeSplit,
|
|
||||||
StringPrototypeStartsWith,
|
StringPrototypeStartsWith,
|
||||||
StringPrototypeToLowerCase,
|
StringPrototypeToLowerCase,
|
||||||
SymbolFor,
|
SymbolFor,
|
||||||
|
@ -268,8 +267,10 @@ class EventSource extends EventTarget {
|
||||||
} else {
|
} else {
|
||||||
let field = chunk;
|
let field = chunk;
|
||||||
let value = "";
|
let value = "";
|
||||||
if (StringPrototypeIncludes(chunk, ":")) {
|
const colonIndex = StringPrototypeIndexOf(chunk, ":");
|
||||||
({ 0: field, 1: value } = StringPrototypeSplit(chunk, ":"));
|
if (colonIndex !== -1) {
|
||||||
|
field = StringPrototypeSlice(chunk, 0, colonIndex);
|
||||||
|
value = StringPrototypeSlice(chunk, colonIndex + 1);
|
||||||
if (StringPrototypeStartsWith(value, " ")) {
|
if (StringPrototypeStartsWith(value, " ")) {
|
||||||
value = StringPrototypeSlice(value, 1);
|
value = StringPrototypeSlice(value, 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ util::unit_test_factory!(
|
||||||
error_stack_test,
|
error_stack_test,
|
||||||
error_test,
|
error_test,
|
||||||
esnext_test,
|
esnext_test,
|
||||||
|
event_source_test,
|
||||||
event_target_test,
|
event_target_test,
|
||||||
event_test,
|
event_test,
|
||||||
fetch_test,
|
fetch_test,
|
||||||
|
|
27
tests/unit/event_source_test.ts
Normal file
27
tests/unit/event_source_test.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEquals } from "./test_util.ts";
|
||||||
|
|
||||||
|
Deno.test(
|
||||||
|
{ permissions: { net: ["127.0.0.1"] } },
|
||||||
|
async function eventSourceColonInMessage() {
|
||||||
|
const portDeferred = Promise.withResolvers<number>();
|
||||||
|
|
||||||
|
await using _server = Deno.serve({
|
||||||
|
handler: () =>
|
||||||
|
new Response('data: {"key":"value"}\n\n', {
|
||||||
|
headers: { "content-type": "text/event-stream" },
|
||||||
|
}),
|
||||||
|
onListen: ({ port }) => portDeferred.resolve(port),
|
||||||
|
hostname: "127.0.0.1",
|
||||||
|
port: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const port = await portDeferred.promise;
|
||||||
|
const eventSource = new EventSource(`http://127.0.0.1:${port}/`);
|
||||||
|
const event = await new Promise<MessageEvent>((resolve) =>
|
||||||
|
eventSource.onmessage = resolve
|
||||||
|
);
|
||||||
|
eventSource.close();
|
||||||
|
assertStrictEquals(event.data, '{"key":"value"}');
|
||||||
|
},
|
||||||
|
);
|
Loading…
Reference in a new issue