Skip to content
This repository was archived by the owner on Nov 9, 2026. It is now read-only.

Commit 9fe3bb4

Browse files
committed
Keep Streams implementations internally consistent
Replace a partial or null host Streams surface as a complete constructor suite so stream products retain compatible instanceof relationships. Preserve a complete suite on repeated initialization.\n\nHarden the native initialization test so N-API failures complete the test promise instead of hanging, and cover partial host replacement plus null handling and cross-constructor compatibility.
1 parent bfcf0fb commit 9fe3bb4

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎Polyfills/Streams/Source/Streams.cpp‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ namespace Babylon::Polyfills::Streams
3131
bool needsPonyfill{};
3232
for (const auto name : constructorNames)
3333
{
34-
if (global.Get(name.data()).IsUndefined())
34+
const auto constructor = global.Get(name.data());
35+
if (constructor.IsUndefined() || constructor.IsNull())
3536
{
3637
needsPonyfill = true;
3738
break;
@@ -46,10 +47,7 @@ namespace Babylon::Polyfills::Streams
4647
const auto exports = Napi::Eval(env, Internal::StreamsScripts::Ponyfill.data(), "jsruntimehost://web-streams-polyfill.js").As<Napi::Object>();
4748
for (const auto name : constructorNames)
4849
{
49-
if (global.Get(name.data()).IsUndefined())
50-
{
51-
global.Set(name.data(), exports.Get(name.data()));
52-
}
50+
global.Set(name.data(), exports.Get(name.data()));
5351
}
5452
}
5553
}

‎Tests/UnitTests/Shared/Shared.cpp‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,50 @@ TEST(Scheduling, IntervalSurvivesThrowingCallback)
170170
EXPECT_EQ(unhandledErrorCount.load(), 2);
171171
}
172172

173-
TEST(Streams, PreservesHostConstructorsAndIsIdempotent)
173+
TEST(Streams, ReplacesPartialHostSuiteAndIsIdempotent)
174174
{
175-
Babylon::AppRuntime runtime{};
176-
std::promise<void> done;
175+
std::promise<std::string> done;
176+
std::atomic_bool completed{};
177+
const auto complete = [&done, &completed](std::string result) {
178+
if (!completed.exchange(true))
179+
{
180+
done.set_value(std::move(result));
181+
}
182+
};
177183

178-
runtime.Dispatch([&done](Napi::Env env) {
184+
Babylon::AppRuntime::Options options{};
185+
options.UnhandledExceptionHandler = [&complete](const Napi::Error& error) {
186+
complete(Napi::GetErrorString(error));
187+
};
188+
Babylon::AppRuntime runtime{options};
189+
190+
runtime.Dispatch([&complete](Napi::Env env) {
179191
auto global = env.Global();
180192
const auto hostReadableStream = Napi::Function::New(env, [](const Napi::CallbackInfo&) {}, "HostReadableStream");
181193
global.Set("ReadableStream", hostReadableStream);
194+
global.Set("TransformStream", env.Null());
182195

183196
Babylon::Polyfills::Streams::Initialize(env);
184-
EXPECT_TRUE(global.Get("ReadableStream").StrictEquals(hostReadableStream));
185-
EXPECT_TRUE(global.Get("TransformStream").IsFunction());
186-
197+
const auto installedReadableStream = global.Get("ReadableStream");
187198
const auto installedTransformStream = global.Get("TransformStream");
199+
EXPECT_FALSE(installedReadableStream.StrictEquals(hostReadableStream));
200+
if (!installedReadableStream.IsFunction() || !installedTransformStream.IsFunction())
201+
{
202+
complete("Streams::Initialize did not install a complete constructor suite.");
203+
return;
204+
}
205+
206+
const auto transform = installedTransformStream.As<Napi::Function>().New({});
207+
EXPECT_TRUE(transform.Get("readable").As<Napi::Object>().InstanceOf(installedReadableStream.As<Napi::Function>()));
208+
188209
Babylon::Polyfills::Streams::Initialize(env);
189-
EXPECT_TRUE(global.Get("ReadableStream").StrictEquals(hostReadableStream));
210+
EXPECT_TRUE(global.Get("ReadableStream").StrictEquals(installedReadableStream));
190211
EXPECT_TRUE(global.Get("TransformStream").StrictEquals(installedTransformStream));
191-
done.set_value();
212+
complete({});
192213
});
193214

194-
done.get_future().get();
215+
const auto error = done.get_future().get();
216+
EXPECT_TRUE(error.empty()) << error;
195217
}
196218

197219
TEST(Compression, PreservesHostConstructorsAndIsIdempotent)

0 commit comments

Comments
 (0)