diff options
-rw-r--r-- | ext/fetch/27_eventsource.js | 7 | ||||
-rw-r--r-- | tests/integration/js_unit_tests.rs | 1 | ||||
-rw-r--r-- | tests/unit/event_source_test.ts | 27 |
3 files changed, 32 insertions, 3 deletions
diff --git a/ext/fetch/27_eventsource.js b/ext/fetch/27_eventsource.js index 02b77e88d..1ab9d8009 100644 --- a/ext/fetch/27_eventsource.js +++ b/ext/fetch/27_eventsource.js @@ -15,7 +15,6 @@ const { StringPrototypeIncludes, StringPrototypeIndexOf, StringPrototypeSlice, - StringPrototypeSplit, StringPrototypeStartsWith, StringPrototypeToLowerCase, SymbolFor, @@ -268,8 +267,10 @@ class EventSource extends EventTarget { } else { let field = chunk; let value = ""; - if (StringPrototypeIncludes(chunk, ":")) { - ({ 0: field, 1: value } = StringPrototypeSplit(chunk, ":")); + const colonIndex = StringPrototypeIndexOf(chunk, ":"); + if (colonIndex !== -1) { + field = StringPrototypeSlice(chunk, 0, colonIndex); + value = StringPrototypeSlice(chunk, colonIndex + 1); if (StringPrototypeStartsWith(value, " ")) { value = StringPrototypeSlice(value, 1); } diff --git a/tests/integration/js_unit_tests.rs b/tests/integration/js_unit_tests.rs index d96af78d8..2bf78034e 100644 --- a/tests/integration/js_unit_tests.rs +++ b/tests/integration/js_unit_tests.rs @@ -30,6 +30,7 @@ util::unit_test_factory!( error_stack_test, error_test, esnext_test, + event_source_test, event_target_test, event_test, fetch_test, diff --git a/tests/unit/event_source_test.ts b/tests/unit/event_source_test.ts new file mode 100644 index 000000000..242c12d6e --- /dev/null +++ b/tests/unit/event_source_test.ts @@ -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"}'); + }, +); |