@depup/react-native-worklets 0.12.1-depup.0 → 0.13.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +1 -2
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +1 -1
- package/Common/cpp/worklets/Compat/StableApi.cpp +24 -8
- package/Common/cpp/worklets/Compat/StableApi.h +1 -1
- package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +113 -50
- package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +10 -1
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +95 -37
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +57 -7
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.cpp +94 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxyInitializer.h +58 -0
- package/Common/cpp/worklets/Networking/Networking.cpp +283 -0
- package/Common/cpp/worklets/Networking/Networking.h +51 -0
- package/Common/cpp/worklets/Networking/NetworkingBackend.h +47 -0
- package/Common/cpp/worklets/Networking/NetworkingInstaller.cpp +127 -0
- package/Common/cpp/worklets/Networking/NetworkingInstaller.h +19 -0
- package/Common/cpp/worklets/Networking/NetworkingTypes.h +33 -0
- package/Common/cpp/worklets/Networking/TextDecoding.cpp +189 -0
- package/Common/cpp/worklets/Networking/TextDecoding.h +29 -0
- package/Common/cpp/worklets/RunLoop/AsyncQueue.h +28 -0
- package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +9 -0
- package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +6 -0
- package/Common/cpp/worklets/RunLoop/EventLoop.cpp +19 -4
- package/Common/cpp/worklets/RunLoop/EventLoop.h +3 -0
- package/Common/cpp/worklets/SharedItems/MemoryManager.cpp +1 -1
- package/Common/cpp/worklets/SharedItems/MemoryManager.h +2 -1
- package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.cpp +37 -0
- package/Common/cpp/worklets/SharedItems/Serializable/CustomSerializable.h +33 -0
- package/Common/cpp/worklets/SharedItems/Serializable/RetainingSerializable.h +112 -0
- package/Common/cpp/worklets/SharedItems/Serializable/Serializable.cpp +129 -0
- package/Common/cpp/worklets/SharedItems/Serializable/Serializable.h +121 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.cpp +38 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableArray.h +39 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.cpp +35 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableArrayBuffer.h +43 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.cpp +23 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableBigInt.h +35 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.cpp +38 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableError.h +30 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.cpp +24 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostFunction.h +32 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.cpp +19 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableHostObject.h +25 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.cpp +29 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableImport.h +25 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.cpp +44 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableMap.h +25 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.cpp +80 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableObject.h +35 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.cpp +22 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableRegExp.h +25 -0
- package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.cpp → Serializable/SerializableRemoteFunction.cpp} +30 -2
- package/Common/cpp/worklets/SharedItems/{SerializableRemoteFunction.h → Serializable/SerializableRemoteFunction.h} +24 -10
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.cpp +46 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableScalar.h +41 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.cpp +36 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableSet.h +23 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.cpp +19 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableString.h +22 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.cpp +26 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableTurboModuleLike.h +33 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.cpp +31 -0
- package/Common/cpp/worklets/SharedItems/Serializable/SerializableWorklet.h +23 -0
- package/Common/cpp/worklets/SharedItems/Serializable.h +21 -409
- package/Common/cpp/worklets/SharedItems/Shareable.cpp +1 -1
- package/Common/cpp/worklets/SharedItems/Shareable.h +1 -1
- package/Common/cpp/worklets/SharedItems/Synchronizable.h +43 -20
- package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +24 -26
- package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +5 -7
- package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.cpp +36 -0
- package/Common/cpp/worklets/SharedItems/SynchronizableDynamic.h +27 -0
- package/Common/cpp/worklets/SharedItems/SynchronizableFixed.cpp +76 -0
- package/Common/cpp/worklets/SharedItems/SynchronizableFixed.h +48 -0
- package/Common/cpp/worklets/Tools/ScriptBuffer.h +0 -5
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +16 -30
- package/Common/cpp/worklets/WorkletRuntime/RuntimeBindings.h +27 -30
- package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +4 -3
- package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +2 -1
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +79 -29
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +34 -19
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +13 -86
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +1 -7
- package/Package.swift +147 -0
- package/README.md +7 -7
- package/RNWorklets.podspec +1 -2
- package/android/CMakeLists.txt +7 -26
- package/android/build.gradle.kts +5 -31
- package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +46 -129
- package/android/src/main/cpp/worklets/android/WorkletsModule.h +21 -11
- package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +2 -4
- package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.cpp +82 -0
- package/android/src/main/cpp/worklets/android/networking/AndroidNetworkingBackend.h +32 -0
- package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.cpp +73 -0
- package/android/src/main/cpp/worklets/android/networking/JNetworkRequestListener.h +38 -0
- package/android/src/{no-networking → main/java}/com/swmansion/worklets/WorkletsModule.kt +40 -28
- package/android/src/main/java/com/swmansion/worklets/networking/NetworkRequestListener.kt +37 -0
- package/android/src/main/java/com/swmansion/worklets/networking/Networking.kt +244 -0
- package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.kt +40 -5
- package/apple/worklets/apple/AnimationFrameQueue.mm +9 -1
- package/apple/worklets/apple/AssertJavaScriptQueue.h +3 -2
- package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +4 -0
- package/apple/worklets/apple/IOSUIScheduler.mm +3 -0
- package/apple/worklets/apple/Networking/AppleNetworkingBackend.h +29 -0
- package/apple/worklets/apple/Networking/AppleNetworkingBackend.mm +93 -0
- package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.h +37 -0
- package/apple/worklets/apple/Networking/WorkletsURLSessionDelegate.mm +447 -0
- package/apple/worklets/apple/WorkletsModule.h +3 -2
- package/apple/worklets/apple/WorkletsModule.mm +82 -74
- package/bundleMode/index.js +10 -47
- package/bundleMode/polyfills/prepareBundleMode.js +16 -0
- package/changes.json +6 -6
- package/compatibility.json +4 -1
- package/lib/module/WorkletsModule/NativeWorklets.native.js +12 -9
- package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
- package/lib/module/debug/jsVersion.js +1 -1
- package/lib/module/featureFlags/staticFlags.json +0 -1
- package/lib/module/featureFlags/types.js +0 -1
- package/lib/module/featureFlags/types.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/initializers/initializers.native.js +5 -12
- package/lib/module/initializers/initializers.native.js.map +1 -1
- package/lib/module/memory/bundleUnpacker.native.js +2 -10
- package/lib/module/memory/bundleUnpacker.native.js.map +1 -1
- package/lib/module/memory/serializable.native.js +3 -31
- package/lib/module/memory/serializable.native.js.map +1 -1
- package/lib/module/memory/synchronizable.js +1 -1
- package/lib/module/memory/synchronizable.js.map +1 -1
- package/lib/module/memory/synchronizable.native.js +7 -3
- package/lib/module/memory/synchronizable.native.js.map +1 -1
- package/lib/module/memory/synchronizableUnpacker.native.js +22 -9
- package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -1
- package/lib/module/memory/valueUnpacker.native.js +0 -8
- package/lib/module/memory/valueUnpacker.native.js.map +1 -1
- package/lib/module/mock.js +22 -1
- package/lib/module/mock.js.map +1 -1
- package/lib/module/networking/AbortController.js +43 -0
- package/lib/module/networking/AbortController.js.map +1 -0
- package/lib/module/networking/Blob.js +60 -0
- package/lib/module/networking/Blob.js.map +1 -0
- package/lib/module/networking/DOMException.js +35 -0
- package/lib/module/networking/DOMException.js.map +1 -0
- package/lib/module/networking/FileReader.js +83 -0
- package/lib/module/networking/FileReader.js.map +1 -0
- package/lib/module/networking/FormData.js +105 -0
- package/lib/module/networking/FormData.js.map +1 -0
- package/lib/module/networking/XMLHttpRequest.js +450 -0
- package/lib/module/networking/XMLHttpRequest.js.map +1 -0
- package/lib/module/networking/bytes.js +6 -0
- package/lib/module/networking/bytes.js.map +1 -0
- package/lib/module/networking/events.js +44 -0
- package/lib/module/networking/events.js.map +1 -0
- package/lib/module/networking/http.js +100 -0
- package/lib/module/networking/http.js.map +1 -0
- package/lib/module/networking/install.js +37 -0
- package/lib/module/networking/install.js.map +1 -0
- package/lib/module/networking/utf8.js +99 -0
- package/lib/module/networking/utf8.js.map +1 -0
- package/lib/module/privateGlobals.d.js +0 -1
- package/lib/module/privateGlobals.d.js.map +1 -1
- package/lib/module/runLoop/common/queueMicrotaskPolyfill.js +9 -0
- package/lib/module/runLoop/common/queueMicrotaskPolyfill.js.map +1 -0
- package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +7 -3
- package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -1
- package/lib/module/runLoop/workletRuntime/index.js +1 -1
- package/lib/module/runLoop/workletRuntime/index.js.map +1 -1
- package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +1 -1
- package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -1
- package/lib/module/runLoop/workletRuntime/taskQueue.js +0 -13
- package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -1
- package/lib/module/runtimes.native.js +8 -1
- package/lib/module/runtimes.native.js.map +1 -1
- package/lib/module/specs/NativeWorkletsModule.js.map +1 -1
- package/lib/module/threads.js +11 -0
- package/lib/module/threads.js.map +1 -1
- package/lib/module/threads.native.js +35 -42
- package/lib/module/threads.native.js.map +1 -1
- package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +6 -5
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
- package/lib/typescript/debug/jsVersion.d.ts +1 -1
- package/lib/typescript/featureFlags/types.d.ts +0 -1
- package/lib/typescript/featureFlags/types.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +2 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/memory/bundleUnpacker.native.d.ts.map +1 -1
- package/lib/typescript/memory/serializable.native.d.ts.map +1 -1
- package/lib/typescript/memory/synchronizable.d.ts +6 -2
- package/lib/typescript/memory/synchronizable.d.ts.map +1 -1
- package/lib/typescript/memory/synchronizable.native.d.ts +6 -2
- package/lib/typescript/memory/synchronizable.native.d.ts.map +1 -1
- package/lib/typescript/memory/synchronizableUnpacker.native.d.ts +1 -1
- package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -1
- package/lib/typescript/memory/types.d.ts +6 -0
- package/lib/typescript/memory/types.d.ts.map +1 -1
- package/lib/typescript/memory/valueUnpacker.native.d.ts.map +1 -1
- package/lib/typescript/networking/AbortController.d.ts +16 -0
- package/lib/typescript/networking/AbortController.d.ts.map +1 -0
- package/lib/typescript/networking/Blob.d.ts +16 -0
- package/lib/typescript/networking/Blob.d.ts.map +1 -0
- package/lib/typescript/networking/DOMException.d.ts +5 -0
- package/lib/typescript/networking/DOMException.d.ts.map +1 -0
- package/lib/typescript/networking/FileReader.d.ts +19 -0
- package/lib/typescript/networking/FileReader.d.ts.map +1 -0
- package/lib/typescript/networking/FormData.d.ts +30 -0
- package/lib/typescript/networking/FormData.d.ts.map +1 -0
- package/lib/typescript/networking/XMLHttpRequest.d.ts +77 -0
- package/lib/typescript/networking/XMLHttpRequest.d.ts.map +1 -0
- package/lib/typescript/networking/bytes.d.ts +2 -0
- package/lib/typescript/networking/bytes.d.ts.map +1 -0
- package/lib/typescript/networking/events.d.ts +18 -0
- package/lib/typescript/networking/events.d.ts.map +1 -0
- package/lib/typescript/networking/http.d.ts +14 -0
- package/lib/typescript/networking/http.d.ts.map +1 -0
- package/lib/typescript/networking/install.d.ts +2 -0
- package/lib/typescript/networking/install.d.ts.map +1 -0
- package/lib/typescript/networking/utf8.d.ts +4 -0
- package/lib/typescript/networking/utf8.d.ts.map +1 -0
- package/lib/typescript/runLoop/{workletRuntime/queueMicrotask.d.ts → common/queueMicrotaskPolyfill.d.ts} +1 -1
- package/lib/typescript/runLoop/common/queueMicrotaskPolyfill.d.ts.map +1 -0
- package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -1
- package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +0 -2
- package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -1
- package/lib/typescript/runtimes.native.d.ts.map +1 -1
- package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -0
- package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -1
- package/lib/typescript/threads.d.ts +8 -0
- package/lib/typescript/threads.d.ts.map +1 -1
- package/lib/typescript/threads.native.d.ts +8 -1
- package/lib/typescript/threads.native.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +9 -5
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +36 -22
- package/plugin/index.js +63 -93
- package/plugin-oxc/babel.d.ts +7 -0
- package/plugin-oxc/babel.js +200 -0
- package/plugin-oxc/index.d.ts +33 -0
- package/plugin-oxc/index.js +36 -0
- package/plugin-oxc/worklets-oxc-plugin.darwin-arm64.node +0 -0
- package/plugin-oxc/worklets-oxc-plugin.darwin-x64.node +0 -0
- package/plugin-oxc/worklets-oxc-plugin.linux-arm64.node +0 -0
- package/plugin-oxc/worklets-oxc-plugin.linux-x64.node +0 -0
- package/plugin-oxc/worklets-oxc-plugin.win32-arm64.node +0 -0
- package/plugin-oxc/worklets-oxc-plugin.win32-x64.node +0 -0
- package/react-native.config.js +5 -0
- package/scripts/worklets_utils.rb +0 -10
- package/src/WorkletsModule/NativeWorklets.native.ts +26 -9
- package/src/WorkletsModule/workletsModuleProxy.ts +15 -5
- package/src/debug/jsVersion.ts +1 -1
- package/src/featureFlags/staticFlags.json +0 -1
- package/src/featureFlags/types.ts +0 -1
- package/src/index.ts +3 -0
- package/src/initializers/initializers.native.ts +5 -13
- package/src/memory/bundleUnpacker.native.ts +6 -13
- package/src/memory/serializable.native.ts +6 -46
- package/src/memory/synchronizable.native.ts +38 -4
- package/src/memory/synchronizable.ts +22 -2
- package/src/memory/synchronizableUnpacker.native.ts +38 -13
- package/src/memory/types.ts +9 -0
- package/src/memory/valueUnpacker.native.ts +0 -8
- package/src/mock.ts +28 -1
- package/src/networking/AbortController.ts +57 -0
- package/src/networking/Blob.ts +74 -0
- package/src/networking/DOMException.ts +37 -0
- package/src/networking/FileReader.ts +99 -0
- package/src/networking/FormData.ts +135 -0
- package/src/networking/XMLHttpRequest.ts +557 -0
- package/src/networking/bytes.ts +8 -0
- package/src/networking/events.ts +61 -0
- package/src/networking/http.ts +173 -0
- package/src/networking/install.ts +38 -0
- package/src/networking/utf8.ts +122 -0
- package/src/privateGlobals.d.ts +20 -13
- package/src/runLoop/common/queueMicrotaskPolyfill.ts +12 -0
- package/src/runLoop/uiRuntime/requestAnimationFrame.ts +7 -4
- package/src/runLoop/workletRuntime/index.ts +1 -1
- package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +1 -1
- package/src/runLoop/workletRuntime/taskQueue.ts +0 -15
- package/src/runtimes.native.ts +10 -1
- package/src/specs/NativeWorkletsModule.ts +1 -0
- package/src/threads.native.ts +49 -54
- package/src/threads.ts +11 -0
- package/src/types.ts +9 -5
- package/Common/cpp/worklets/SharedItems/Serializable.cpp +0 -373
- package/Common/cpp/worklets/SharedItems/SerializableFactory.cpp +0 -172
- package/Common/cpp/worklets/SharedItems/SerializableFactory.h +0 -85
- package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +0 -59
- package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.cpp +0 -51
- package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.h +0 -43
- package/android/src/networking/com/swmansion/worklets/WorkletRuntimeWrapper.kt +0 -27
- package/android/src/networking/com/swmansion/worklets/WorkletsHeaderUtil.kt +0 -30
- package/android/src/networking/com/swmansion/worklets/WorkletsModule.kt +0 -203
- package/android/src/networking/com/swmansion/worklets/WorkletsNetworkEventUtil.kt +0 -272
- package/android/src/networking/com/swmansion/worklets/WorkletsNetworking.kt +0 -1124
- package/android/src/networking/com/swmansion/worklets/WorkletsOkHttpCallUtil.kt +0 -40
- package/android/src/networking/com/swmansion/worklets/WorkletsProgressListener.kt +0 -13
- package/android/src/networking/com/swmansion/worklets/WorkletsProgressRequestBody.kt +0 -98
- package/android/src/networking/com/swmansion/worklets/WorkletsProgressResponseBody.kt +0 -60
- package/android/src/networking/com/swmansion/worklets/WorkletsProgressiveStringDecoder.kt +0 -87
- package/android/src/networking/com/swmansion/worklets/WorkletsRequestBodyUtil.kt +0 -181
- package/apple/worklets/apple/Networking/WorkletsNetworking.h +0 -25
- package/apple/worklets/apple/Networking/WorkletsNetworking.mm +0 -707
- package/bundleMode/shims/turboModuleRegistryShim.js +0 -47
- package/lib/module/bundleMode/network.native.js +0 -44
- package/lib/module/bundleMode/network.native.js.map +0 -1
- package/lib/module/runLoop/workletRuntime/queueMicrotask.js +0 -11
- package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +0 -1
- package/lib/typescript/bundleMode/network.native.d.ts +0 -7
- package/lib/typescript/bundleMode/network.native.d.ts.map +0 -1
- package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +0 -1
- package/src/bundleMode/network.native.ts +0 -63
- package/src/runLoop/workletRuntime/queueMicrotask.ts +0 -13
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { Blob } from './Blob';
|
|
4
|
+
import { toArrayBuffer } from './bytes';
|
|
5
|
+
import { DOMException } from './DOMException';
|
|
6
|
+
import type { NetworkingEventListener } from './events';
|
|
7
|
+
import { EventTargetLite } from './events';
|
|
8
|
+
import { FormData } from './FormData';
|
|
9
|
+
import {
|
|
10
|
+
combineHeader,
|
|
11
|
+
extractCharset,
|
|
12
|
+
getHeader,
|
|
13
|
+
hasHeader,
|
|
14
|
+
isForbiddenMethod,
|
|
15
|
+
isForbiddenRequestHeader,
|
|
16
|
+
isHeaderName,
|
|
17
|
+
isHeaderValue,
|
|
18
|
+
isSupportedRequestUrl,
|
|
19
|
+
isToken,
|
|
20
|
+
normalizeHeaderValue,
|
|
21
|
+
normalizeMethod,
|
|
22
|
+
sortAndCombineHeaders,
|
|
23
|
+
} from './http';
|
|
24
|
+
import { stripUtf8Bom, utf8Decode, utf8Encode } from './utf8';
|
|
25
|
+
|
|
26
|
+
const UNSENT = 0;
|
|
27
|
+
const OPENED = 1;
|
|
28
|
+
const HEADERS_RECEIVED = 2;
|
|
29
|
+
const LOADING = 3;
|
|
30
|
+
const DONE = 4;
|
|
31
|
+
|
|
32
|
+
const RESPONSE_TYPES = ['', 'text', 'arraybuffer', 'blob', 'json'] as const;
|
|
33
|
+
|
|
34
|
+
type XMLHttpRequestResponseType = (typeof RESPONSE_TYPES)[number];
|
|
35
|
+
|
|
36
|
+
type ResponsePayload = {
|
|
37
|
+
status: number;
|
|
38
|
+
statusText: string;
|
|
39
|
+
headers: Array<[string, string]>;
|
|
40
|
+
url: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type ProgressPayload = {
|
|
44
|
+
loaded: number;
|
|
45
|
+
total: number;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
type DonePayload = {
|
|
49
|
+
body?: ArrayBuffer;
|
|
50
|
+
error?: 'timeout' | 'aborted' | 'network';
|
|
51
|
+
message?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
class XMLHttpRequestUpload extends EventTargetLite {}
|
|
55
|
+
|
|
56
|
+
export class XMLHttpRequest extends EventTargetLite {
|
|
57
|
+
static readonly UNSENT = UNSENT;
|
|
58
|
+
static readonly OPENED = OPENED;
|
|
59
|
+
static readonly HEADERS_RECEIVED = HEADERS_RECEIVED;
|
|
60
|
+
static readonly LOADING = LOADING;
|
|
61
|
+
static readonly DONE = DONE;
|
|
62
|
+
|
|
63
|
+
readonly UNSENT = UNSENT;
|
|
64
|
+
readonly OPENED = OPENED;
|
|
65
|
+
readonly HEADERS_RECEIVED = HEADERS_RECEIVED;
|
|
66
|
+
readonly LOADING = LOADING;
|
|
67
|
+
readonly DONE = DONE;
|
|
68
|
+
|
|
69
|
+
readyState = UNSENT;
|
|
70
|
+
status = 0;
|
|
71
|
+
statusText = '';
|
|
72
|
+
responseURL = '';
|
|
73
|
+
timeout = 0;
|
|
74
|
+
/**
|
|
75
|
+
* The specification defaults this to `false`. React Native's own
|
|
76
|
+
* `XMLHttpRequest` defaults it to `true`, and Worklet Runtimes share the
|
|
77
|
+
* app's cookie storage, so the parity is deliberate.
|
|
78
|
+
*/
|
|
79
|
+
withCredentials = true;
|
|
80
|
+
readonly upload = new XMLHttpRequestUpload();
|
|
81
|
+
|
|
82
|
+
onreadystatechange: NetworkingEventListener | null = null;
|
|
83
|
+
onloadstart: NetworkingEventListener | null = null;
|
|
84
|
+
onprogress: NetworkingEventListener | null = null;
|
|
85
|
+
onload: NetworkingEventListener | null = null;
|
|
86
|
+
onloadend: NetworkingEventListener | null = null;
|
|
87
|
+
onerror: NetworkingEventListener | null = null;
|
|
88
|
+
ontimeout: NetworkingEventListener | null = null;
|
|
89
|
+
onabort: NetworkingEventListener | null = null;
|
|
90
|
+
|
|
91
|
+
private requestId: number | null = null;
|
|
92
|
+
private requestToken = 0;
|
|
93
|
+
private method = 'GET';
|
|
94
|
+
private url = '';
|
|
95
|
+
private sent = false;
|
|
96
|
+
private hasRequestBody = false;
|
|
97
|
+
private requestBodyLength = 0;
|
|
98
|
+
private uploadSettled = false;
|
|
99
|
+
private downloadedBytes = 0;
|
|
100
|
+
private mimeTypeOverride: string | null = null;
|
|
101
|
+
private requestHeaders: Array<[string, string]> = [];
|
|
102
|
+
private responseHeaders: Array<[string, string]> = [];
|
|
103
|
+
private responseTypeValue: XMLHttpRequestResponseType = '';
|
|
104
|
+
private responseBytes: ArrayBuffer | null = null;
|
|
105
|
+
private responseBlobValue: Blob | null = null;
|
|
106
|
+
private responseTextValue: string | null = null;
|
|
107
|
+
private responseJsonValue: unknown = undefined;
|
|
108
|
+
|
|
109
|
+
get responseType(): XMLHttpRequestResponseType {
|
|
110
|
+
return this.responseTypeValue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
set responseType(value: XMLHttpRequestResponseType) {
|
|
114
|
+
if (this.readyState >= LOADING) {
|
|
115
|
+
throw new DOMException(
|
|
116
|
+
"[Worklets] Failed to set 'responseType' on 'XMLHttpRequest': the response type cannot be changed once loading has started.",
|
|
117
|
+
'InvalidStateError'
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (!RESPONSE_TYPES.includes(value)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.responseTypeValue = value;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get response(): unknown {
|
|
127
|
+
switch (this.responseTypeValue) {
|
|
128
|
+
case '':
|
|
129
|
+
case 'text':
|
|
130
|
+
return this.readyState >= LOADING ? this.getResponseText() : '';
|
|
131
|
+
case 'arraybuffer':
|
|
132
|
+
return this.readyState === DONE ? this.responseBytes : null;
|
|
133
|
+
case 'blob':
|
|
134
|
+
if (this.readyState !== DONE || this.responseBytes === null) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
if (this.responseBlobValue === null) {
|
|
138
|
+
this.responseBlobValue = new Blob([this.responseBytes], {
|
|
139
|
+
type:
|
|
140
|
+
this.mimeTypeOverride ??
|
|
141
|
+
this.getResponseHeader('content-type') ??
|
|
142
|
+
'',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return this.responseBlobValue;
|
|
146
|
+
case 'json':
|
|
147
|
+
if (this.readyState !== DONE) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
if (this.responseJsonValue === undefined) {
|
|
151
|
+
try {
|
|
152
|
+
this.responseJsonValue = JSON.parse(
|
|
153
|
+
this.responseBytes === null
|
|
154
|
+
? ''
|
|
155
|
+
: utf8Decode(stripUtf8Bom(new Uint8Array(this.responseBytes)))
|
|
156
|
+
);
|
|
157
|
+
} catch {
|
|
158
|
+
this.responseJsonValue = null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return this.responseJsonValue;
|
|
162
|
+
default:
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get responseText(): string {
|
|
168
|
+
if (this.responseTypeValue !== '' && this.responseTypeValue !== 'text') {
|
|
169
|
+
throw new DOMException(
|
|
170
|
+
"[Worklets] Failed to read 'responseText' on 'XMLHttpRequest': the value is only accessible if the object's 'responseType' is '' or 'text'.",
|
|
171
|
+
'InvalidStateError'
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
return this.readyState >= LOADING ? this.getResponseText() : '';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
open(method: string, url: string, async = true) {
|
|
178
|
+
const methodValue = String(method);
|
|
179
|
+
const urlValue = String(url);
|
|
180
|
+
if (!isToken(methodValue)) {
|
|
181
|
+
throw new DOMException(
|
|
182
|
+
`[Worklets] Failed to execute 'open' on 'XMLHttpRequest': '${methodValue}' is not a valid HTTP method.`,
|
|
183
|
+
'SyntaxError'
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (isForbiddenMethod(methodValue)) {
|
|
187
|
+
throw new DOMException(
|
|
188
|
+
`[Worklets] Failed to execute 'open' on 'XMLHttpRequest': '${methodValue}' HTTP method is unsupported.`,
|
|
189
|
+
'SecurityError'
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
if (urlValue === '') {
|
|
193
|
+
throw new DOMException(
|
|
194
|
+
"[Worklets] Failed to execute 'open' on 'XMLHttpRequest': the URL cannot be empty.",
|
|
195
|
+
'SyntaxError'
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (!async) {
|
|
199
|
+
throw new DOMException(
|
|
200
|
+
'[Worklets] Synchronous XMLHttpRequest is not supported.',
|
|
201
|
+
'InvalidAccessError'
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
this.terminate();
|
|
205
|
+
this.method = normalizeMethod(methodValue);
|
|
206
|
+
this.url = urlValue;
|
|
207
|
+
this.sent = false;
|
|
208
|
+
this.hasRequestBody = false;
|
|
209
|
+
this.requestBodyLength = 0;
|
|
210
|
+
this.uploadSettled = false;
|
|
211
|
+
this.mimeTypeOverride = null;
|
|
212
|
+
this.requestHeaders = [];
|
|
213
|
+
this.resetResponse();
|
|
214
|
+
if (this.readyState !== OPENED) {
|
|
215
|
+
this.setReadyState(OPENED);
|
|
216
|
+
} else {
|
|
217
|
+
this.readyState = OPENED;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
setRequestHeader(name: string, value: string) {
|
|
222
|
+
if (this.readyState !== OPENED || this.sent) {
|
|
223
|
+
throw new DOMException(
|
|
224
|
+
"[Worklets] Failed to execute 'setRequestHeader' on 'XMLHttpRequest': the object's state must be OPENED.",
|
|
225
|
+
'InvalidStateError'
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
const nameValue = String(name);
|
|
229
|
+
const headerValue = normalizeHeaderValue(String(value));
|
|
230
|
+
if (!isHeaderName(nameValue) || !isHeaderValue(headerValue)) {
|
|
231
|
+
throw new DOMException(
|
|
232
|
+
`[Worklets] Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '${nameValue}: ${headerValue}' is not a valid header.`,
|
|
233
|
+
'SyntaxError'
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
if (isForbiddenRequestHeader(nameValue, headerValue)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
combineHeader(this.requestHeaders, nameValue, headerValue);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
getAllResponseHeaders(): string {
|
|
243
|
+
return sortAndCombineHeaders(this.responseHeaders);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
getResponseHeader(name: string): string | null {
|
|
247
|
+
return getHeader(this.responseHeaders, String(name));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
overrideMimeType(mimeType: string) {
|
|
251
|
+
if (this.readyState >= LOADING) {
|
|
252
|
+
throw new DOMException(
|
|
253
|
+
"[Worklets] Failed to execute 'overrideMimeType' on 'XMLHttpRequest': the MIME type cannot be overridden once loading has started.",
|
|
254
|
+
'InvalidStateError'
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
this.mimeTypeOverride = String(mimeType);
|
|
258
|
+
this.responseTextValue = null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
send(body?: unknown) {
|
|
262
|
+
const networking = globalThis.__workletsNetworking;
|
|
263
|
+
if (networking === undefined) {
|
|
264
|
+
throw new DOMException(
|
|
265
|
+
'[Worklets] XMLHttpRequest is not available on this runtime.',
|
|
266
|
+
'InvalidStateError'
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (this.readyState !== OPENED || this.sent) {
|
|
270
|
+
throw new DOMException(
|
|
271
|
+
"[Worklets] Failed to execute 'send' on 'XMLHttpRequest': the object's state must be OPENED.",
|
|
272
|
+
'InvalidStateError'
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
this.sent = true;
|
|
276
|
+
|
|
277
|
+
if (!isSupportedRequestUrl(this.url)) {
|
|
278
|
+
this.__dispatch('loadstart');
|
|
279
|
+
setTimeout(() =>
|
|
280
|
+
this.handleRequestError(
|
|
281
|
+
'network',
|
|
282
|
+
`Only http and https URLs are supported, got '${this.url}'.`
|
|
283
|
+
)
|
|
284
|
+
);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const { data, contentType } =
|
|
289
|
+
this.method === 'GET' || this.method === 'HEAD'
|
|
290
|
+
? { data: undefined, contentType: undefined }
|
|
291
|
+
: normalizeBody(body);
|
|
292
|
+
const headers = [...this.requestHeaders];
|
|
293
|
+
if (contentType !== undefined && !hasHeader(headers, 'content-type')) {
|
|
294
|
+
headers.push(['Content-Type', contentType]);
|
|
295
|
+
}
|
|
296
|
+
this.hasRequestBody = data !== undefined;
|
|
297
|
+
this.requestBodyLength =
|
|
298
|
+
typeof data === 'string'
|
|
299
|
+
? utf8Encode(data).length
|
|
300
|
+
: (data?.byteLength ?? 0);
|
|
301
|
+
|
|
302
|
+
const token = ++this.requestToken;
|
|
303
|
+
this.requestId = networking.sendRequest(
|
|
304
|
+
{
|
|
305
|
+
method: this.method,
|
|
306
|
+
url: this.url,
|
|
307
|
+
headers,
|
|
308
|
+
body: data,
|
|
309
|
+
timeoutMs: this.timeout,
|
|
310
|
+
withCredentials: this.withCredentials,
|
|
311
|
+
},
|
|
312
|
+
(type, payload) => {
|
|
313
|
+
if (token === this.requestToken && this.requestId !== null) {
|
|
314
|
+
this.handleNetworkingEvent(type, payload);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
this.__dispatch('loadstart');
|
|
320
|
+
if (this.hasRequestBody) {
|
|
321
|
+
this.upload.__dispatch('loadstart', {
|
|
322
|
+
loaded: 0,
|
|
323
|
+
total: this.requestBodyLength,
|
|
324
|
+
lengthComputable: this.requestBodyLength > 0,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
abort() {
|
|
330
|
+
const wasInFlight = this.terminate();
|
|
331
|
+
if (this.readyState === OPENED && !this.sent) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (!wasInFlight || this.readyState === UNSENT) {
|
|
335
|
+
this.readyState = UNSENT;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (this.readyState === DONE) {
|
|
339
|
+
this.resetResponse();
|
|
340
|
+
this.readyState = UNSENT;
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
this.resetResponse();
|
|
344
|
+
this.setReadyState(DONE);
|
|
345
|
+
this.settleUploadWithError('abort');
|
|
346
|
+
this.__dispatch('abort');
|
|
347
|
+
this.__dispatch('loadend');
|
|
348
|
+
if (this.readyState === DONE) {
|
|
349
|
+
this.readyState = UNSENT;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
private handleNetworkingEvent(type: string, payload: unknown) {
|
|
354
|
+
switch (type) {
|
|
355
|
+
case 'response': {
|
|
356
|
+
const { status, statusText, headers, url } = payload as ResponsePayload;
|
|
357
|
+
this.status = status;
|
|
358
|
+
this.statusText = statusText;
|
|
359
|
+
this.responseHeaders = headers;
|
|
360
|
+
this.responseURL = url;
|
|
361
|
+
this.settleUploadWithSuccess();
|
|
362
|
+
this.setReadyState(HEADERS_RECEIVED);
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case 'downloadProgress': {
|
|
366
|
+
const { loaded, total } = payload as ProgressPayload;
|
|
367
|
+
this.downloadedBytes = loaded;
|
|
368
|
+
if (this.readyState === HEADERS_RECEIVED) {
|
|
369
|
+
this.setReadyState(LOADING);
|
|
370
|
+
} else {
|
|
371
|
+
this.__dispatch('readystatechange');
|
|
372
|
+
}
|
|
373
|
+
this.__dispatch('progress', {
|
|
374
|
+
loaded,
|
|
375
|
+
total: total > 0 ? total : 0,
|
|
376
|
+
lengthComputable: total > 0,
|
|
377
|
+
});
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
case 'uploadProgress': {
|
|
381
|
+
const { loaded, total } = payload as ProgressPayload;
|
|
382
|
+
this.upload.__dispatch('progress', {
|
|
383
|
+
loaded,
|
|
384
|
+
total: total > 0 ? total : 0,
|
|
385
|
+
lengthComputable: total > 0,
|
|
386
|
+
});
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case 'done': {
|
|
390
|
+
this.requestId = null;
|
|
391
|
+
const { body, error, message } = payload as DonePayload;
|
|
392
|
+
if (error !== undefined) {
|
|
393
|
+
this.handleRequestError(error, message);
|
|
394
|
+
} else if (this.status === 0) {
|
|
395
|
+
this.handleRequestError(
|
|
396
|
+
'network',
|
|
397
|
+
'The request completed without a response.'
|
|
398
|
+
);
|
|
399
|
+
} else {
|
|
400
|
+
this.handleResponseEndOfBody(body);
|
|
401
|
+
}
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private handleRequestError(
|
|
408
|
+
error: 'timeout' | 'aborted' | 'network',
|
|
409
|
+
message?: string
|
|
410
|
+
) {
|
|
411
|
+
const terminalEvent =
|
|
412
|
+
error === 'timeout' ? 'timeout' : error === 'aborted' ? 'abort' : 'error';
|
|
413
|
+
this.resetResponse();
|
|
414
|
+
this.setReadyState(DONE);
|
|
415
|
+
this.settleUploadWithError(terminalEvent);
|
|
416
|
+
this.__dispatch(terminalEvent, { message });
|
|
417
|
+
this.__dispatch('loadend');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private handleResponseEndOfBody(body?: ArrayBuffer) {
|
|
421
|
+
this.responseBytes = body ?? new ArrayBuffer(0);
|
|
422
|
+
this.responseTextValue = null;
|
|
423
|
+
this.responseBlobValue = null;
|
|
424
|
+
this.responseJsonValue = undefined;
|
|
425
|
+
const transmitted = Math.max(
|
|
426
|
+
this.responseBytes.byteLength,
|
|
427
|
+
this.downloadedBytes
|
|
428
|
+
);
|
|
429
|
+
const rawContentLength = this.getResponseHeader('content-length');
|
|
430
|
+
const contentLength =
|
|
431
|
+
rawContentLength === null ? Number.NaN : Number(rawContentLength);
|
|
432
|
+
const total =
|
|
433
|
+
Number.isInteger(contentLength) && contentLength > 0 ? contentLength : 0;
|
|
434
|
+
const progress = {
|
|
435
|
+
loaded: transmitted,
|
|
436
|
+
total,
|
|
437
|
+
lengthComputable: total > 0,
|
|
438
|
+
};
|
|
439
|
+
this.settleUploadWithSuccess();
|
|
440
|
+
this.__dispatch('progress', progress);
|
|
441
|
+
this.setReadyState(DONE);
|
|
442
|
+
this.__dispatch('load', progress);
|
|
443
|
+
this.__dispatch('loadend', progress);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private settleUploadWithSuccess() {
|
|
447
|
+
if (!this.hasRequestBody || this.uploadSettled) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.uploadSettled = true;
|
|
451
|
+
const progress = {
|
|
452
|
+
loaded: this.requestBodyLength,
|
|
453
|
+
total: this.requestBodyLength,
|
|
454
|
+
lengthComputable: this.requestBodyLength > 0,
|
|
455
|
+
};
|
|
456
|
+
this.upload.__dispatch('progress', progress);
|
|
457
|
+
this.upload.__dispatch('load', progress);
|
|
458
|
+
this.upload.__dispatch('loadend', progress);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private settleUploadWithError(type: string) {
|
|
462
|
+
if (!this.hasRequestBody || this.uploadSettled) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
this.uploadSettled = true;
|
|
466
|
+
this.upload.__dispatch(type);
|
|
467
|
+
this.upload.__dispatch('loadend');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
private getResponseText(): string {
|
|
471
|
+
if (this.responseTextValue === null) {
|
|
472
|
+
this.responseTextValue = decodeResponseText(
|
|
473
|
+
this.responseBytes,
|
|
474
|
+
extractCharset(this.mimeTypeOverride) ??
|
|
475
|
+
extractCharset(this.getResponseHeader('content-type'))
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
return this.responseTextValue;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private resetResponse() {
|
|
482
|
+
this.status = 0;
|
|
483
|
+
this.statusText = '';
|
|
484
|
+
this.responseURL = '';
|
|
485
|
+
this.downloadedBytes = 0;
|
|
486
|
+
this.responseHeaders = [];
|
|
487
|
+
this.responseBytes = null;
|
|
488
|
+
this.responseTextValue = null;
|
|
489
|
+
this.responseBlobValue = null;
|
|
490
|
+
this.responseJsonValue = undefined;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private terminate(): boolean {
|
|
494
|
+
if (this.requestId === null) {
|
|
495
|
+
return this.sent;
|
|
496
|
+
}
|
|
497
|
+
globalThis.__workletsNetworking?.abortRequest(this.requestId);
|
|
498
|
+
this.requestId = null;
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
private setReadyState(readyState: number) {
|
|
503
|
+
this.readyState = readyState;
|
|
504
|
+
this.__dispatch('readystatechange');
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function decodeResponseText(
|
|
509
|
+
bytes: ArrayBuffer | null,
|
|
510
|
+
charset?: string
|
|
511
|
+
): string {
|
|
512
|
+
if (bytes === null || bytes.byteLength === 0) {
|
|
513
|
+
return '';
|
|
514
|
+
}
|
|
515
|
+
const networking = globalThis.__workletsNetworking;
|
|
516
|
+
if (networking !== undefined) {
|
|
517
|
+
return networking.decodeText(bytes, charset);
|
|
518
|
+
}
|
|
519
|
+
return utf8Decode(new Uint8Array(bytes));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function isBlobLike(body: unknown): body is Blob {
|
|
523
|
+
return typeof (body as Blob | undefined)?.__getBytes === 'function';
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function normalizeBody(body: unknown): {
|
|
527
|
+
data?: string | ArrayBuffer;
|
|
528
|
+
contentType?: string;
|
|
529
|
+
} {
|
|
530
|
+
if (body === null || body === undefined) {
|
|
531
|
+
return {};
|
|
532
|
+
}
|
|
533
|
+
if (typeof body === 'string') {
|
|
534
|
+
return { data: body, contentType: 'text/plain;charset=UTF-8' };
|
|
535
|
+
}
|
|
536
|
+
if (body instanceof ArrayBuffer) {
|
|
537
|
+
return { data: body };
|
|
538
|
+
}
|
|
539
|
+
if (isBlobLike(body)) {
|
|
540
|
+
return {
|
|
541
|
+
data: toArrayBuffer(body.__getBytes()),
|
|
542
|
+
contentType: body.type !== '' ? body.type : undefined,
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
if (body instanceof FormData) {
|
|
546
|
+
const { body: data, contentType } = body.__encodeMultipart();
|
|
547
|
+
return { data, contentType };
|
|
548
|
+
}
|
|
549
|
+
if (ArrayBuffer.isView(body)) {
|
|
550
|
+
return {
|
|
551
|
+
data: toArrayBuffer(
|
|
552
|
+
new Uint8Array(body.buffer, body.byteOffset, body.byteLength)
|
|
553
|
+
),
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return { data: String(body as { toString(): string }) };
|
|
557
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
interface NetworkingEvent {
|
|
4
|
+
type: string;
|
|
5
|
+
target: EventTargetLite;
|
|
6
|
+
loaded: number;
|
|
7
|
+
total: number;
|
|
8
|
+
lengthComputable: boolean;
|
|
9
|
+
message?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type NetworkingEventListener = (event: NetworkingEvent) => void;
|
|
13
|
+
|
|
14
|
+
export class EventTargetLite {
|
|
15
|
+
private eventListeners = new Map<string, Set<NetworkingEventListener>>();
|
|
16
|
+
|
|
17
|
+
addEventListener(type: string, listener: NetworkingEventListener) {
|
|
18
|
+
let listeners = this.eventListeners.get(type);
|
|
19
|
+
if (listeners === undefined) {
|
|
20
|
+
listeners = new Set();
|
|
21
|
+
this.eventListeners.set(type, listeners);
|
|
22
|
+
}
|
|
23
|
+
listeners.add(listener);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
removeEventListener(type: string, listener: NetworkingEventListener) {
|
|
27
|
+
this.eventListeners.get(type)?.delete(listener);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
__dispatch(type: string, extra?: Partial<NetworkingEvent>) {
|
|
31
|
+
const event: NetworkingEvent = {
|
|
32
|
+
type,
|
|
33
|
+
target: this,
|
|
34
|
+
loaded: 0,
|
|
35
|
+
total: 0,
|
|
36
|
+
lengthComputable: false,
|
|
37
|
+
...extra,
|
|
38
|
+
};
|
|
39
|
+
const handler = (this as unknown as Record<string, unknown>)[`on${type}`];
|
|
40
|
+
if (typeof handler === 'function') {
|
|
41
|
+
this.invokeListener(handler as NetworkingEventListener, event);
|
|
42
|
+
}
|
|
43
|
+
const listeners = this.eventListeners.get(type);
|
|
44
|
+
if (listeners !== undefined) {
|
|
45
|
+
for (const listener of Array.from(listeners)) {
|
|
46
|
+
this.invokeListener(listener, event);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private invokeListener(
|
|
52
|
+
listener: NetworkingEventListener,
|
|
53
|
+
event: NetworkingEvent
|
|
54
|
+
) {
|
|
55
|
+
try {
|
|
56
|
+
listener.call(this, event);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|