-
Notifications
You must be signed in to change notification settings - Fork 23
Add optional WHATWG Streams polyfill #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthargett
wants to merge
2
commits into
BabylonJS:main
Choose a base branch
from
rebeckerspecialties:web-streams-polyfill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| set(WEB_STREAMS_POLYFILL_FILE | ||
| "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/web-streams-polyfill/ponyfill.es5.js") | ||
| set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS | ||
| "${WEB_STREAMS_POLYFILL_FILE}") | ||
| file(READ "${WEB_STREAMS_POLYFILL_FILE}" WEB_STREAMS_POLYFILL_SOURCE) | ||
| string(SUBSTRING "${WEB_STREAMS_POLYFILL_SOURCE}" 0 60000 WEB_STREAMS_POLYFILL_SOURCE_1) | ||
| string(SUBSTRING "${WEB_STREAMS_POLYFILL_SOURCE}" 60000 -1 WEB_STREAMS_POLYFILL_SOURCE_2) | ||
| configure_file( | ||
| "Source/StreamsScripts.h.in" | ||
| "${CMAKE_CURRENT_BINARY_DIR}/Generated/StreamsScripts.h" | ||
| @ONLY) | ||
|
|
||
| set(SOURCES | ||
| "Include/Babylon/Polyfills/Streams.h" | ||
| "Source/Streams.cpp" | ||
| "Source/StreamsScripts.h.in" | ||
| "ThirdParty/web-streams-polyfill/LICENSE" | ||
| "ThirdParty/web-streams-polyfill/ponyfill.es5.js") | ||
|
|
||
| add_library(Streams ${SOURCES}) | ||
| warnings_as_errors(Streams) | ||
|
|
||
| target_include_directories(Streams | ||
| PUBLIC "Include" | ||
| PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Generated") | ||
|
|
||
| target_link_libraries(Streams PUBLIC JsRuntime) | ||
|
|
||
| set_property(TARGET Streams PROPERTY FOLDER Polyfills) | ||
| source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include <Babylon/Api.h> | ||
| #include <napi/env.h> | ||
|
|
||
| namespace Babylon::Polyfills::Streams | ||
| { | ||
| // Installs the WHATWG Streams constructors that are not already supplied | ||
| // by the JavaScript engine. | ||
| void BABYLON_API Initialize(Napi::Env env); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Web Streams | ||
|
|
||
| Installs the standard `ReadableStream`, `WritableStream`, `TransformStream`, | ||
| reader, writer, controller, and queuing-strategy globals when the selected | ||
| JavaScript engine does not provide them. | ||
|
|
||
| The implementation is the ES5 ponyfill bundle from | ||
| [`web-streams-polyfill` 4.3.0](https://github.com/MattiasBuelens/web-streams-polyfill/tree/add69059ed08eae6a18559aba49575a280d1529e), | ||
| which is based on the WHATWG reference implementation. The vendored project | ||
| tests this release against the Streams portion of WPT at revision | ||
| [`c05b4473`](https://github.com/web-platform-tests/wpt/tree/c05b447326585237713013c66341eab2cdf967b6/streams). | ||
|
|
||
| `Initialize` preserves any Streams constructors already supplied by the host | ||
| engine and fills only missing globals. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <Babylon/Polyfills/Streams.h> | ||
|
|
||
| #include "StreamsScripts.h" | ||
|
|
||
| #include <array> | ||
| #include <string_view> | ||
|
|
||
| namespace Babylon::Polyfills::Streams | ||
| { | ||
| void BABYLON_API Initialize(Napi::Env env) | ||
| { | ||
| Napi::HandleScope scope{env}; | ||
| auto global = env.Global(); | ||
|
|
||
| static constexpr std::array<std::string_view, 13> constructorNames{ | ||
| "ReadableStream", | ||
| "ReadableStreamDefaultController", | ||
| "ReadableByteStreamController", | ||
| "ReadableStreamBYOBRequest", | ||
| "ReadableStreamDefaultReader", | ||
| "ReadableStreamBYOBReader", | ||
| "WritableStream", | ||
| "WritableStreamDefaultController", | ||
| "WritableStreamDefaultWriter", | ||
| "ByteLengthQueuingStrategy", | ||
| "CountQueuingStrategy", | ||
| "TransformStream", | ||
| "TransformStreamDefaultController", | ||
| }; | ||
|
|
||
| bool needsPonyfill{}; | ||
| for (const auto name : constructorNames) | ||
| { | ||
| const auto constructor = global.Get(name.data()); | ||
| if (constructor.IsUndefined() || constructor.IsNull()) | ||
| { | ||
| needsPonyfill = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!needsPonyfill) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| const auto exports = Napi::Eval(env, Internal::StreamsScripts::Ponyfill.data(), "jsruntimehost://web-streams-polyfill.js").As<Napi::Object>(); | ||
| for (const auto name : constructorNames) | ||
| { | ||
| global.Set(name.data(), exports.Get(name.data())); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
|
|
||
| namespace Babylon::Polyfills::Internal::StreamsScripts | ||
| { | ||
| inline constexpr char PonyfillPart1[] = R"JSRHSTREAM( | ||
| (function() { | ||
| var exports = {}; | ||
| var module = { exports: exports }; | ||
| @WEB_STREAMS_POLYFILL_SOURCE_1@ | ||
| )JSRHSTREAM"; | ||
|
|
||
| inline constexpr char PonyfillPart2[] = R"JSRHSTREAM(@WEB_STREAMS_POLYFILL_SOURCE_2@ | ||
| return module.exports; | ||
| })() | ||
| )JSRHSTREAM"; | ||
|
|
||
| template<std::size_t Part1Size, std::size_t Part2Size> | ||
| consteval auto Join(const char (&part1)[Part1Size], const char (&part2)[Part2Size]) | ||
| { | ||
| std::array<char, Part1Size + Part2Size - 1> result{}; | ||
| for (std::size_t index{}; index < Part1Size - 1; ++index) | ||
| { | ||
| result[index] = part1[index]; | ||
| } | ||
| for (std::size_t index{}; index < Part2Size; ++index) | ||
| { | ||
| result[Part1Size - 1 + index] = part2[index]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| inline constexpr auto Ponyfill = Join(PonyfillPart1, PonyfillPart2); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2026 Mattias Buelens | ||
| Copyright (c) 2016 Diwank Singh Tomer | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
8 changes: 8 additions & 0 deletions
8
Polyfills/Streams/ThirdParty/web-streams-polyfill/ponyfill.es5.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.