Skip to content

Commit

Permalink
fix(ext/fetch): do not truncate field value in EventSource
Browse files Browse the repository at this point in the history
  • Loading branch information
0f-0b committed Feb 10, 2024
1 parent f5e46c9 commit 0662c23
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ext/fetch/27_eventsource.js
Expand Up @@ -16,7 +16,6 @@ const {
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
StringPrototypeToLowerCase,
Symbol,
Expand Down Expand Up @@ -289,10 +288,12 @@ class EventSource extends EventTarget {
} else if (StringPrototypeStartsWith(chunk, ":")) {
continue;
} else {
const colonIndex = StringPrototypeIndexOf(chunk, ":");
let field = chunk;
let value = "";
if (StringPrototypeIncludes(chunk, ":")) {
({ 0: field, 1: value } = StringPrototypeSplit(chunk, ":"));
if (colonIndex !== -1) {
field = StringPrototypeSlice(chunk, 0, colonIndex);
value = StringPrototypeSlice(chunk, colonIndex + 1);
if (StringPrototypeStartsWith(value, " ")) {
value = StringPrototypeSlice(value, 1);
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/js_unit_tests.rs
Expand Up @@ -29,6 +29,7 @@ util::unit_test_factory!(
error_stack_test,
error_test,
esnext_test,
event_source_test,
event_target_test,
event_test,
fetch_test,
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/event_source_test.ts
@@ -0,0 +1,30 @@
// 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: () => {
const readable = ReadableStream.from([
new TextEncoder().encode('data: {"key":"value"}\n\n'),
]);
return new Response(readable, {
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
);
assertStrictEquals(event.data, '{"key":"value"}');
},
);

0 comments on commit 0662c23

Please sign in to comment.