Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ option(JSRUNTIMEHOST_CORE_SCRIPTLOADER "Include JsRuntimeHost Core ScriptLoader"
option(JSRUNTIMEHOST_POLYFILL_CONSOLE "Include JsRuntimeHost Polyfill Console." ON)
option(JSRUNTIMEHOST_POLYFILL_SCHEDULING "Include JsRuntimeHost Polyfill Scheduling." ON)
option(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST "Include JsRuntimeHost Polyfill XMLHttpRequest." ON)
option(JSRUNTIMEHOST_POLYFILL_FETCH "Include JsRuntimeHost Polyfill fetch." ON)
option(JSRUNTIMEHOST_POLYFILL_URL "Include JsRuntimeHost Polyfill URL and URLSearchParams." ON)
option(JSRUNTIMEHOST_POLYFILL_ABORT_CONTROLLER "Include JsRuntimeHost Polyfills AbortController and AbortSignal." ON)
option(JSRUNTIMEHOST_POLYFILL_WEBSOCKET "Include JsRuntimeHost Polyfill WebSocket." ON)
Expand Down Expand Up @@ -140,7 +141,7 @@ endif()
FetchContent_MakeAvailable_With_Message(arcana.cpp)
set_property(TARGET arcana PROPERTY FOLDER Dependencies)

if(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST)
if(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST OR JSRUNTIMEHOST_POLYFILL_FETCH)
FetchContent_MakeAvailable_With_Message(UrlLib)
set_property(TARGET UrlLib PROPERTY FOLDER Dependencies)
endif()
Expand Down
4 changes: 4 additions & 0 deletions Polyfills/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ if(JSRUNTIMEHOST_POLYFILL_XMLHTTPREQUEST)
add_subdirectory(XMLHttpRequest)
endif()

if(JSRUNTIMEHOST_POLYFILL_FETCH)
add_subdirectory(Fetch)
endif()

if(JSRUNTIMEHOST_POLYFILL_URL)
add_subdirectory(URL)
endif()
Expand Down
17 changes: 17 additions & 0 deletions Polyfills/Fetch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set(SOURCES
"Include/Babylon/Polyfills/Fetch.h"
"Source/Fetch.h"
"Source/Fetch.cpp")

add_library(Fetch ${SOURCES})
warnings_as_errors(Fetch)

target_include_directories(Fetch PUBLIC "Include")

target_link_libraries(Fetch
PUBLIC JsRuntime
PRIVATE arcana
PRIVATE UrlLib)

set_property(TARGET Fetch PROPERTY FOLDER Polyfills)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES})
9 changes: 9 additions & 0 deletions Polyfills/Fetch/Include/Babylon/Polyfills/Fetch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <napi/env.h>
#include <Babylon/Api.h>

namespace Babylon::Polyfills::Fetch
{
void BABYLON_API Initialize(Napi::Env env);
}
30 changes: 30 additions & 0 deletions Polyfills/Fetch/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Fetch
Minimal implementation of the [WHATWG `fetch()`](https://fetch.spec.whatwg.org/) API. Like `XMLHttpRequest`, it is implemented on top of the platform-specific transports in the `UrlLib` dependency, so network behavior (libcurl / WinHTTP / etc.) is identical between the two polyfills.

```js
const response = await fetch("https://example.com/data.json");
if (response.ok) {
const data = await response.json();
}
```

## Response
`fetch()` returns a `Promise` that resolves to a `Response`-like object exposing:
* `ok`, `status`, `statusText`, `url`, `redirected`, `type`, `bodyUsed`
* `headers` with `get(name)`, `has(name)`, and `forEach(callback)` (header names are matched case-insensitively)
* `text()`, `arrayBuffer()`, `json()`, `blob()` (each returns a `Promise`)
* `clone()`

The response body is fully buffered before the promise resolves. The body accessors may therefore be called more than once (`bodyUsed` is always reported as `false`), which is a deliberate, lenient deviation from the spec's single-use semantics.

`blob()` requires the `Blob` polyfill to be initialized; otherwise the returned promise rejects.

## Local files
Like `XMLHttpRequest`, `fetch()` supports loading local resources:
* `file:///` loads from an absolute path
* `app:///` loads from a path relative to the current program or package depending on platform

## Other things to be aware of
* Only `GET` and `POST` methods are supported (a `UrlLib` limitation shared with `XMLHttpRequest`).
* Only string request bodies are supported.
* Consistent with the fetch spec, the promise rejects only on transport-level failures. A completed request with a non-`2xx` status (e.g. `404`) still resolves, with `response.ok === false`.
Loading
Loading