@dittolive/ditto 4.7.4-rc.2 → 4.7.4
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/DittoReactNative.podspec +27 -0
- package/README.md +2 -2
- package/node/ditto.cjs.js +1 -1
- package/node/ditto.darwin-arm64.node +0 -0
- package/node/ditto.darwin-x64.node +0 -0
- package/node/ditto.linux-arm.node +0 -0
- package/node/ditto.linux-arm64.node +0 -0
- package/node/ditto.linux-x64.node +0 -0
- package/node/ditto.win32-x64.node +0 -0
- package/node/transports.darwin-arm64.node +0 -0
- package/node/transports.darwin-x64.node +0 -0
- package/package.json +5 -2
- package/react-native/android/.gradle/8.9/checksums/checksums.lock +0 -0
- package/react-native/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
- package/react-native/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
- package/react-native/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
- package/react-native/android/.gradle/8.9/gc.properties +0 -0
- package/react-native/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/react-native/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/react-native/android/.gradle/vcs-1/gc.properties +0 -0
- package/react-native/android/CMakeLists.txt +36 -0
- package/react-native/android/build.gradle +190 -0
- package/react-native/android/cpp-adapter.cpp +259 -0
- package/react-native/android/gradle.properties +5 -0
- package/react-native/android/src/main/AndroidManifest.xml +4 -0
- package/react-native/android/src/main/java/com/dittolive/rnsdk/DittoRNSDKModule.java +120 -0
- package/react-native/android/src/main/java/com/dittolive/rnsdk/DittoRNSDKPackage.java +28 -0
- package/react-native/cpp/include/Arc.hpp +159 -0
- package/react-native/cpp/include/Attachment.h +20 -0
- package/react-native/cpp/include/Authentication.h +23 -0
- package/react-native/cpp/include/Collection.h +13 -0
- package/react-native/cpp/include/ConnectionRequest.h +18 -0
- package/react-native/cpp/include/DQL.h +21 -0
- package/react-native/cpp/include/Document.h +17 -0
- package/react-native/cpp/include/FFIUtils.h +16 -0
- package/react-native/cpp/include/IO.h +13 -0
- package/react-native/cpp/include/Identity.h +17 -0
- package/react-native/cpp/include/Lifecycle.h +16 -0
- package/react-native/cpp/include/LiveQuery.h +17 -0
- package/react-native/cpp/include/Logger.h +22 -0
- package/react-native/cpp/include/Misc.h +30 -0
- package/react-native/cpp/include/Presence.h +18 -0
- package/react-native/cpp/include/SmallPeerInfo.h +19 -0
- package/react-native/cpp/include/Transports.h +25 -0
- package/react-native/cpp/include/TypedArray.hpp +167 -0
- package/react-native/cpp/include/Utils.h +70 -0
- package/react-native/cpp/include/main.h +10 -0
- package/react-native/cpp/src/Attachment.cpp +272 -0
- package/react-native/cpp/src/Authentication.cpp +227 -0
- package/react-native/cpp/src/Collection.cpp +56 -0
- package/react-native/cpp/src/ConnectionRequest.cpp +123 -0
- package/react-native/cpp/src/DQL.cpp +256 -0
- package/react-native/cpp/src/Document.cpp +146 -0
- package/react-native/cpp/src/FFIUtils.cpp +122 -0
- package/react-native/cpp/src/IO.cpp +35 -0
- package/react-native/cpp/src/Identity.cpp +122 -0
- package/react-native/cpp/src/Lifecycle.cpp +93 -0
- package/react-native/cpp/src/LiveQuery.cpp +63 -0
- package/react-native/cpp/src/Logger.cpp +199 -0
- package/react-native/cpp/src/Misc.cpp +322 -0
- package/react-native/cpp/src/Presence.cpp +166 -0
- package/react-native/cpp/src/SmallPeerInfo.cpp +142 -0
- package/react-native/cpp/src/Transports.cpp +275 -0
- package/react-native/cpp/src/TypedArray.cpp +303 -0
- package/react-native/cpp/src/Utils.cpp +139 -0
- package/react-native/cpp/src/main.cpp +178 -0
- package/react-native/dittoffi/dittoffi.h +4873 -0
- package/react-native/dittoffi/ifaddrs.cpp +385 -0
- package/react-native/dittoffi/ifaddrs.h +206 -0
- package/react-native/ios/DittoRNSDK.h +7 -0
- package/react-native/ios/DittoRNSDK.mm +159 -0
- package/react-native/ios/YeetJSIUtils.h +60 -0
- package/react-native/ios/YeetJSIUtils.mm +196 -0
- package/react-native/lib/commonjs/ditto.rn.js +93 -0
- package/react-native/lib/commonjs/ditto.rn.js.map +1 -0
- package/react-native/lib/commonjs/index.js +61 -0
- package/react-native/lib/commonjs/index.js.map +1 -0
- package/react-native/lib/module/ditto.rn.js +89 -0
- package/react-native/lib/module/ditto.rn.js.map +1 -0
- package/react-native/lib/module/index.js +27 -0
- package/react-native/lib/module/index.js.map +1 -0
- package/react-native/lib/typescript/ditto.rn.d.ts +15 -0
- package/react-native/lib/typescript/ditto.rn.d.ts.map +1 -0
- package/react-native/lib/typescript/index.d.ts +1 -0
- package/react-native/lib/typescript/index.d.ts.map +1 -0
- package/react-native/src/ditto.rn.ts +123 -0
- package/react-native/src/environment/environment.fallback.ts +4 -0
- package/react-native/src/index.ts +29 -0
- package/react-native/src/sources/@cbor-redux.ts +2 -0
- package/react-native/src/sources/@ditto.core.ts +1 -0
- package/react-native/src/sources/@environment.ts +1 -0
- package/react-native/src/sources/attachment-fetch-event.ts +54 -0
- package/react-native/src/sources/attachment-fetcher-manager.ts +145 -0
- package/react-native/src/sources/attachment-fetcher.ts +265 -0
- package/react-native/src/sources/attachment-token.ts +129 -0
- package/react-native/src/sources/attachment.ts +121 -0
- package/react-native/src/sources/augment.ts +108 -0
- package/react-native/src/sources/authenticator.ts +314 -0
- package/react-native/src/sources/base-pending-cursor-operation.ts +255 -0
- package/react-native/src/sources/base-pending-id-specific-operation.ts +112 -0
- package/react-native/src/sources/bridge.ts +557 -0
- package/react-native/src/sources/build-time-constants.ts +8 -0
- package/react-native/src/sources/cbor.ts +20 -0
- package/react-native/src/sources/collection-interface.ts +73 -0
- package/react-native/src/sources/collection.ts +219 -0
- package/react-native/src/sources/collections-event.ts +99 -0
- package/react-native/src/sources/connection-request.ts +142 -0
- package/react-native/src/sources/counter.ts +82 -0
- package/react-native/src/sources/ditto.ts +991 -0
- package/react-native/src/sources/document-id.ts +163 -0
- package/react-native/src/sources/document-path.ts +308 -0
- package/react-native/src/sources/document.ts +237 -0
- package/react-native/src/sources/epilogue.ts +32 -0
- package/react-native/src/sources/error-codes.ts +114 -0
- package/react-native/src/sources/error.ts +256 -0
- package/react-native/src/sources/essentials.ts +81 -0
- package/react-native/src/sources/ffi-error.ts +134 -0
- package/react-native/src/sources/ffi.ts +2190 -0
- package/react-native/src/sources/identity.ts +163 -0
- package/react-native/src/sources/init.ts +71 -0
- package/react-native/src/sources/internal.ts +143 -0
- package/react-native/src/sources/keep-alive.ts +73 -0
- package/react-native/src/sources/key-path.ts +198 -0
- package/react-native/src/sources/live-query-event.ts +208 -0
- package/react-native/src/sources/live-query-manager.ts +110 -0
- package/react-native/src/sources/live-query.ts +167 -0
- package/react-native/src/sources/logger.ts +196 -0
- package/react-native/src/sources/main.ts +61 -0
- package/react-native/src/sources/observer-manager.ts +185 -0
- package/react-native/src/sources/observer.ts +79 -0
- package/react-native/src/sources/pending-collections-operation.ts +241 -0
- package/react-native/src/sources/pending-cursor-operation.ts +218 -0
- package/react-native/src/sources/pending-id-specific-operation.ts +218 -0
- package/react-native/src/sources/presence-manager.ts +170 -0
- package/react-native/src/sources/presence.ts +427 -0
- package/react-native/src/sources/query-result-item.ts +131 -0
- package/react-native/src/sources/query-result.ts +55 -0
- package/react-native/src/sources/register.ts +95 -0
- package/react-native/src/sources/small-peer-info.ts +166 -0
- package/react-native/src/sources/static-tcp-client.ts +8 -0
- package/react-native/src/sources/store-observer.ts +170 -0
- package/react-native/src/sources/store.ts +630 -0
- package/react-native/src/sources/subscription-manager.ts +99 -0
- package/react-native/src/sources/subscription.ts +89 -0
- package/react-native/src/sources/sync-subscription.ts +90 -0
- package/react-native/src/sources/sync.ts +561 -0
- package/react-native/src/sources/test-helpers.ts +24 -0
- package/react-native/src/sources/transport-conditions-manager.ts +104 -0
- package/react-native/src/sources/transport-config.ts +430 -0
- package/react-native/src/sources/update-result.ts +66 -0
- package/react-native/src/sources/update-results-map.ts +65 -0
- package/react-native/src/sources/websocket-client.ts +7 -0
- package/react-native/src/sources/write-transaction-collection.ts +122 -0
- package/react-native/src/sources/write-transaction-pending-cursor-operation.ts +101 -0
- package/react-native/src/sources/write-transaction-pending-id-specific-operation.ts +74 -0
- package/react-native/src/sources/write-transaction.ts +121 -0
- package/react-native.config.js +9 -0
- package/web/ditto.es6.js +1 -1
- package/web/ditto.umd.js +1 -1
- package/web/ditto.wasm +0 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <jsi/jsi.h>
|
|
5
|
+
|
|
6
|
+
namespace jsi = facebook::jsi;
|
|
7
|
+
|
|
8
|
+
enum class TypedArrayKind {
|
|
9
|
+
Int8Array,
|
|
10
|
+
Int16Array,
|
|
11
|
+
Int32Array,
|
|
12
|
+
Uint8Array,
|
|
13
|
+
Uint8ClampedArray,
|
|
14
|
+
Uint16Array,
|
|
15
|
+
Uint32Array,
|
|
16
|
+
Float32Array,
|
|
17
|
+
Float64Array,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
template <TypedArrayKind T>
|
|
21
|
+
class TypedArray;
|
|
22
|
+
|
|
23
|
+
template <TypedArrayKind T>
|
|
24
|
+
struct typedArrayTypeMap;
|
|
25
|
+
template <>
|
|
26
|
+
struct typedArrayTypeMap<TypedArrayKind::Int8Array> {
|
|
27
|
+
typedef int8_t type;
|
|
28
|
+
};
|
|
29
|
+
template <>
|
|
30
|
+
struct typedArrayTypeMap<TypedArrayKind::Int16Array> {
|
|
31
|
+
typedef int16_t type;
|
|
32
|
+
};
|
|
33
|
+
template <>
|
|
34
|
+
struct typedArrayTypeMap<TypedArrayKind::Int32Array> {
|
|
35
|
+
typedef int32_t type;
|
|
36
|
+
};
|
|
37
|
+
template <>
|
|
38
|
+
struct typedArrayTypeMap<TypedArrayKind::Uint8Array> {
|
|
39
|
+
typedef uint8_t type;
|
|
40
|
+
};
|
|
41
|
+
template <>
|
|
42
|
+
struct typedArrayTypeMap<TypedArrayKind::Uint8ClampedArray> {
|
|
43
|
+
typedef uint8_t type;
|
|
44
|
+
};
|
|
45
|
+
template <>
|
|
46
|
+
struct typedArrayTypeMap<TypedArrayKind::Uint16Array> {
|
|
47
|
+
typedef uint16_t type;
|
|
48
|
+
};
|
|
49
|
+
template <>
|
|
50
|
+
struct typedArrayTypeMap<TypedArrayKind::Uint32Array> {
|
|
51
|
+
typedef uint32_t type;
|
|
52
|
+
};
|
|
53
|
+
template <>
|
|
54
|
+
struct typedArrayTypeMap<TypedArrayKind::Float32Array> {
|
|
55
|
+
typedef float type;
|
|
56
|
+
};
|
|
57
|
+
template <>
|
|
58
|
+
struct typedArrayTypeMap<TypedArrayKind::Float64Array> {
|
|
59
|
+
typedef double type;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Instance of this class will invalidate PropNameIDCache when destructor is called.
|
|
63
|
+
// Attach this object to global in specific jsi::Runtime to make sure lifecycle of
|
|
64
|
+
// the cache object is connected to the lifecycle of the js runtime
|
|
65
|
+
class InvalidateCacheOnDestroy : public jsi::HostObject {
|
|
66
|
+
public:
|
|
67
|
+
InvalidateCacheOnDestroy(jsi::Runtime &runtime);
|
|
68
|
+
virtual ~InvalidateCacheOnDestroy();
|
|
69
|
+
virtual jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) {
|
|
70
|
+
return jsi::Value::null();
|
|
71
|
+
}
|
|
72
|
+
virtual void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) {}
|
|
73
|
+
virtual std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private:
|
|
78
|
+
uintptr_t key;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
class TypedArrayBase : public jsi::Object {
|
|
82
|
+
public:
|
|
83
|
+
template <TypedArrayKind T>
|
|
84
|
+
using ContentType = typename typedArrayTypeMap<T>::type;
|
|
85
|
+
|
|
86
|
+
TypedArrayBase(jsi::Runtime &, size_t, TypedArrayKind);
|
|
87
|
+
TypedArrayBase(jsi::Runtime &, const jsi::Object &);
|
|
88
|
+
TypedArrayBase(TypedArrayBase &&) = default;
|
|
89
|
+
TypedArrayBase &operator=(TypedArrayBase &&) = default;
|
|
90
|
+
|
|
91
|
+
TypedArrayKind getKind(jsi::Runtime &runtime) const;
|
|
92
|
+
|
|
93
|
+
template <TypedArrayKind T>
|
|
94
|
+
TypedArray<T> get(jsi::Runtime &runtime) const &;
|
|
95
|
+
template <TypedArrayKind T>
|
|
96
|
+
TypedArray<T> get(jsi::Runtime &runtime) &&;
|
|
97
|
+
template <TypedArrayKind T>
|
|
98
|
+
TypedArray<T> as(jsi::Runtime &runtime) const &;
|
|
99
|
+
template <TypedArrayKind T>
|
|
100
|
+
TypedArray<T> as(jsi::Runtime &runtime) &&;
|
|
101
|
+
|
|
102
|
+
size_t size(jsi::Runtime &runtime) const;
|
|
103
|
+
size_t length(jsi::Runtime &runtime) const;
|
|
104
|
+
size_t byteLength(jsi::Runtime &runtime) const;
|
|
105
|
+
size_t byteOffset(jsi::Runtime &runtime) const;
|
|
106
|
+
bool hasBuffer(jsi::Runtime &runtime) const;
|
|
107
|
+
|
|
108
|
+
std::vector<uint8_t> toVector(jsi::Runtime &runtime);
|
|
109
|
+
jsi::ArrayBuffer getBuffer(jsi::Runtime &runtime) const;
|
|
110
|
+
|
|
111
|
+
private:
|
|
112
|
+
template <TypedArrayKind>
|
|
113
|
+
friend class TypedArray;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
bool isTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
|
|
117
|
+
TypedArrayBase getTypedArray(jsi::Runtime &runtime, const jsi::Object &jsObj);
|
|
118
|
+
|
|
119
|
+
std::vector<uint8_t> arrayBufferToVector(jsi::Runtime &runtime, jsi::Object &jsObj);
|
|
120
|
+
void arrayBufferUpdate(
|
|
121
|
+
jsi::Runtime &runtime,
|
|
122
|
+
jsi::ArrayBuffer &buffer,
|
|
123
|
+
std::vector<uint8_t> data,
|
|
124
|
+
size_t offset);
|
|
125
|
+
|
|
126
|
+
template <TypedArrayKind T>
|
|
127
|
+
class TypedArray : public TypedArrayBase {
|
|
128
|
+
public:
|
|
129
|
+
TypedArray(jsi::Runtime &runtime, size_t size);
|
|
130
|
+
TypedArray(jsi::Runtime &runtime, std::vector<ContentType<T>> data);
|
|
131
|
+
TypedArray(TypedArrayBase &&base);
|
|
132
|
+
TypedArray(TypedArray &&) = default;
|
|
133
|
+
TypedArray &operator=(TypedArray &&) = default;
|
|
134
|
+
|
|
135
|
+
std::vector<ContentType<T>> toVector(jsi::Runtime &runtime);
|
|
136
|
+
void update(jsi::Runtime &runtime, const std::vector<ContentType<T>> &data);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
template <TypedArrayKind T>
|
|
140
|
+
TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) const & {
|
|
141
|
+
assert(getKind(runtime) == T);
|
|
142
|
+
(void)runtime; // when assert is disabled we need to mark this as used
|
|
143
|
+
return TypedArray<T>(jsi::Value(runtime, jsi::Value(runtime, *this).asObject(runtime)));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
template <TypedArrayKind T>
|
|
147
|
+
TypedArray<T> TypedArrayBase::get(jsi::Runtime &runtime) && {
|
|
148
|
+
assert(getKind(runtime) == T);
|
|
149
|
+
(void)runtime; // when assert is disabled we need to mark this as used
|
|
150
|
+
return TypedArray<T>(std::move(*this));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
template <TypedArrayKind T>
|
|
154
|
+
TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) const & {
|
|
155
|
+
if (getKind(runtime) != T) {
|
|
156
|
+
throw jsi::JSError(runtime, "Object is not a TypedArray");
|
|
157
|
+
}
|
|
158
|
+
return get<T>(runtime);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
template <TypedArrayKind T>
|
|
162
|
+
TypedArray<T> TypedArrayBase::as(jsi::Runtime &runtime) && {
|
|
163
|
+
if (getKind(runtime) != T) {
|
|
164
|
+
throw jsi::JSError(runtime, "Object is not a TypedArray");
|
|
165
|
+
}
|
|
166
|
+
return std::move(*this).get<T>(runtime);
|
|
167
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#ifndef UTILS_H
|
|
2
|
+
#define UTILS_H
|
|
3
|
+
|
|
4
|
+
#include <vector>
|
|
5
|
+
#include <jsi/jsi.h>
|
|
6
|
+
#include "TypedArray.hpp"
|
|
7
|
+
#include <dittoffi.h>
|
|
8
|
+
|
|
9
|
+
#define STRINGIFY(it) #it
|
|
10
|
+
|
|
11
|
+
#define STRINGIFIED_FUNC_NAME() STRINGIFY(__function)
|
|
12
|
+
#define STRINGIFY_TYPE(T) #T
|
|
13
|
+
|
|
14
|
+
using namespace facebook::jsi;
|
|
15
|
+
|
|
16
|
+
namespace sharedjsi
|
|
17
|
+
{
|
|
18
|
+
|
|
19
|
+
std::string pointerToHexString(void const *ptr);
|
|
20
|
+
|
|
21
|
+
template <typename T>
|
|
22
|
+
Value cPointerToJSPointerObject(Runtime &runtime, const T *pointer) {
|
|
23
|
+
if (pointer == nullptr) {
|
|
24
|
+
return Value::null();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Object obj(runtime);
|
|
28
|
+
obj.setProperty(runtime, "type", STRINGIFY_TYPE(T));
|
|
29
|
+
obj.setProperty(runtime, "addr", pointerToHexString(pointer));
|
|
30
|
+
return obj;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
template<typename T>
|
|
34
|
+
T *jsPointerObjectToCPointer(Runtime &runtime, const Value &arg)
|
|
35
|
+
{
|
|
36
|
+
if (arg.isNull() || arg.isUndefined()) {
|
|
37
|
+
return nullptr;
|
|
38
|
+
}
|
|
39
|
+
Object obj = arg.getObject(runtime);
|
|
40
|
+
std::string str = obj.getProperty(runtime, "addr").toString(runtime).utf8(runtime);
|
|
41
|
+
T *ptr;
|
|
42
|
+
sscanf(str.c_str(), "%p", &ptr);
|
|
43
|
+
return ptr;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
template<typename T>
|
|
47
|
+
Object cSlicedBoxToJSSlicedBox(Runtime &runtime, T box_c_bytes)
|
|
48
|
+
{
|
|
49
|
+
Object obj(runtime);
|
|
50
|
+
obj.setProperty(runtime, "ptr", cPointerToJSPointerObject<uint8_t>(runtime, box_c_bytes.ptr));
|
|
51
|
+
obj.setProperty(runtime, "len", static_cast<double>(box_c_bytes.len));
|
|
52
|
+
return obj;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
std::vector<uint8_t> jsTypedArrayToCVector(Runtime &runtime, const Value &arg);
|
|
56
|
+
std::string jsTypedArrayToCString(Runtime &runtime, const Value &arg);
|
|
57
|
+
|
|
58
|
+
slice_ref_uint8_t borrowVecAsOptRefSlice(std::vector<uint8_t> const& v);
|
|
59
|
+
slice_ref_uint8_t borrowVecAsRefSlice(std::vector<uint8_t> const& v);
|
|
60
|
+
|
|
61
|
+
Value int64ToBigIntOrNumber(Runtime &runtime, int64_t value);
|
|
62
|
+
Value uint64ToBigIntOrNumber(Runtime &runtime, uint64_t value);
|
|
63
|
+
int64_t bigIntOrNumberToInt64(Runtime &runtime, Value const& value);
|
|
64
|
+
uint64_t bigIntOrNumberToUint64(Runtime &runtime, Value const& value);
|
|
65
|
+
|
|
66
|
+
void ditto_tick(Runtime* jsi);
|
|
67
|
+
void ditto_enqueue_call(std::function<void(Runtime *)> && task);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#endif /* UTILS_H */
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
#include "Utils.h"
|
|
2
|
+
|
|
3
|
+
#include <Attachment.h>
|
|
4
|
+
#include <Arc.hpp>
|
|
5
|
+
|
|
6
|
+
namespace sharedjsi
|
|
7
|
+
{
|
|
8
|
+
|
|
9
|
+
DEFINE_RETAIN_RELEASE_FOR(Function, v_retain, v_release);
|
|
10
|
+
|
|
11
|
+
class AttachmentResolver {
|
|
12
|
+
public:
|
|
13
|
+
Function onCompleteCb;
|
|
14
|
+
Function onProgressCb;
|
|
15
|
+
Function onDeletedCb;
|
|
16
|
+
|
|
17
|
+
AttachmentResolver(Function onComplete, Function onProgress, Function onDeleted)
|
|
18
|
+
: onCompleteCb(std::move(onComplete)), onDeletedCb(std::move(onDeleted)), onProgressCb(std::move(onProgress)) {}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
extern "C" {
|
|
22
|
+
void v_on_complete(void *ctx, AttachmentHandle_t *handle)
|
|
23
|
+
{
|
|
24
|
+
ditto_enqueue_call([=](Runtime *jsi) {
|
|
25
|
+
auto jsObject = reinterpret_cast<AttachmentResolver *>(ctx);
|
|
26
|
+
Runtime &runtime = *jsi;
|
|
27
|
+
jsObject->onCompleteCb.call(runtime, cPointerToJSPointerObject<AttachmentHandle>(runtime, handle));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void v_on_progress(void *ctx, uint64_t downloaded, uint64_t to_download)
|
|
32
|
+
{
|
|
33
|
+
ditto_enqueue_call([=](Runtime *jsi) {
|
|
34
|
+
auto jsObject = reinterpret_cast<AttachmentResolver *>(ctx);
|
|
35
|
+
Runtime &runtime = *jsi;
|
|
36
|
+
jsObject->onProgressCb.call(runtime, uint64ToBigIntOrNumber(runtime, downloaded), uint64ToBigIntOrNumber(runtime, to_download));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
void v_on_deleted(void *ctx)
|
|
41
|
+
{
|
|
42
|
+
ditto_enqueue_call([=](Runtime *jsi) {
|
|
43
|
+
auto jsObject = reinterpret_cast<AttachmentResolver *>(ctx);
|
|
44
|
+
Runtime &runtime = *jsi;
|
|
45
|
+
jsObject->onDeletedCb.call(runtime);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
Function ditto_cancel_resolve_attachment(Runtime &runtime)
|
|
51
|
+
{
|
|
52
|
+
return Function::createFromHostFunction(runtime,
|
|
53
|
+
PropNameID::forAscii(runtime,
|
|
54
|
+
STRINGIFIED_FUNC_NAME()),
|
|
55
|
+
0,
|
|
56
|
+
[](Runtime &runtime,
|
|
57
|
+
const Value &thisValue,
|
|
58
|
+
const Value *arguments,
|
|
59
|
+
size_t count) -> Value
|
|
60
|
+
{
|
|
61
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
62
|
+
std::vector<uint8_t> id_vec = jsTypedArrayToCVector(runtime, arguments[1]);
|
|
63
|
+
slice_ref_uint8_t id = borrowVecAsOptRefSlice(id_vec);
|
|
64
|
+
uint64_t cancelToken = bigIntOrNumberToUint64(runtime, arguments[2]);
|
|
65
|
+
|
|
66
|
+
return static_cast<double>(::ditto_cancel_resolve_attachment(ditto, id, cancelToken));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Function ditto_free_attachment_handle(Runtime &runtime)
|
|
71
|
+
{
|
|
72
|
+
return Function::createFromHostFunction(runtime,
|
|
73
|
+
PropNameID::forAscii(runtime,
|
|
74
|
+
STRINGIFIED_FUNC_NAME()),
|
|
75
|
+
0,
|
|
76
|
+
[](Runtime &runtime,
|
|
77
|
+
const Value &thisValue,
|
|
78
|
+
const Value *arguments,
|
|
79
|
+
size_t count) -> Value
|
|
80
|
+
{
|
|
81
|
+
::ditto_free_attachment_handle(jsPointerObjectToCPointer<AttachmentHandle_t>(runtime, arguments[0]));
|
|
82
|
+
return Value();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Function ditto_get_attachment_status(Runtime &runtime)
|
|
87
|
+
{
|
|
88
|
+
return Function::createFromHostFunction(runtime,
|
|
89
|
+
PropNameID::forAscii(runtime,
|
|
90
|
+
STRINGIFIED_FUNC_NAME()),
|
|
91
|
+
0,
|
|
92
|
+
[](Runtime &runtime,
|
|
93
|
+
const Value &thisValue,
|
|
94
|
+
const Value *arguments,
|
|
95
|
+
size_t count) -> Value
|
|
96
|
+
{
|
|
97
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
98
|
+
std::vector<uint8_t> id_vec = jsTypedArrayToCVector(runtime, arguments[1]);
|
|
99
|
+
slice_ref_uint8_t id = borrowVecAsRefSlice(id_vec);
|
|
100
|
+
|
|
101
|
+
AttachmentHandleResult_t res = ::ditto_get_attachment_status(ditto, id);
|
|
102
|
+
|
|
103
|
+
Object obj(runtime);
|
|
104
|
+
obj.setProperty(runtime, "status_code", static_cast<double>(res.status_code));
|
|
105
|
+
obj.setProperty(runtime, "handle", cPointerToJSPointerObject<AttachmentHandle_t>(runtime, res.handle));
|
|
106
|
+
return obj;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
Function ditto_get_complete_attachment_data(Runtime &runtime)
|
|
111
|
+
{
|
|
112
|
+
return Function::createFromHostFunction(runtime,
|
|
113
|
+
PropNameID::forAscii(runtime,
|
|
114
|
+
STRINGIFIED_FUNC_NAME()),
|
|
115
|
+
0,
|
|
116
|
+
[](Runtime &runtime,
|
|
117
|
+
const Value &thisValue,
|
|
118
|
+
const Value *arguments,
|
|
119
|
+
size_t count) -> Value
|
|
120
|
+
{
|
|
121
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
122
|
+
AttachmentHandle_t* handle = jsPointerObjectToCPointer<AttachmentHandle_t>(runtime, arguments[1]);
|
|
123
|
+
AttachmentDataResult_t res = ::ditto_get_complete_attachment_data(ditto, handle);
|
|
124
|
+
|
|
125
|
+
Object obj(runtime);
|
|
126
|
+
obj.setProperty(runtime, "status_code", static_cast<double>(res.status));
|
|
127
|
+
obj.setProperty(runtime, "data", cSlicedBoxToJSSlicedBox(runtime, res.data));
|
|
128
|
+
return obj;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
Function ditto_get_complete_attachment_path(Runtime &runtime)
|
|
133
|
+
{
|
|
134
|
+
return Function::createFromHostFunction(runtime,
|
|
135
|
+
PropNameID::forAscii(runtime,
|
|
136
|
+
STRINGIFIED_FUNC_NAME()),
|
|
137
|
+
0,
|
|
138
|
+
[](Runtime &runtime,
|
|
139
|
+
const Value &thisValue,
|
|
140
|
+
const Value *arguments,
|
|
141
|
+
size_t count) -> Value
|
|
142
|
+
{
|
|
143
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
144
|
+
AttachmentHandle_t *attachmentHandler = jsPointerObjectToCPointer<AttachmentHandle_t>(runtime, arguments[1]);
|
|
145
|
+
|
|
146
|
+
char *res = ::ditto_get_complete_attachment_path(ditto, attachmentHandler);
|
|
147
|
+
return cPointerToJSPointerObject<char>(runtime, res);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
Function ditto_new_attachment_from_bytes(Runtime &runtime)
|
|
152
|
+
{
|
|
153
|
+
return Function::createFromHostFunction(runtime,
|
|
154
|
+
PropNameID::forAscii(runtime,
|
|
155
|
+
STRINGIFIED_FUNC_NAME()),
|
|
156
|
+
0,
|
|
157
|
+
[](Runtime &runtime,
|
|
158
|
+
const Value &thisValue,
|
|
159
|
+
const Value *arguments,
|
|
160
|
+
size_t count) -> Value
|
|
161
|
+
{
|
|
162
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
163
|
+
std::vector<uint8_t> bytes_vec = jsTypedArrayToCVector(runtime, arguments[1]);
|
|
164
|
+
slice_ref_uint8_t bytes = borrowVecAsRefSlice(bytes_vec);
|
|
165
|
+
|
|
166
|
+
CAttachment_t c_attachment;
|
|
167
|
+
uint32_t res = ::ditto_new_attachment_from_bytes(ditto, bytes, &c_attachment);
|
|
168
|
+
|
|
169
|
+
if (res != 0) {
|
|
170
|
+
// It failed, bubble up the error.
|
|
171
|
+
return static_cast<double>(res);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
Object attachment = arguments[2].getObject(runtime);
|
|
175
|
+
|
|
176
|
+
attachment.setProperty(runtime, "len", uint64ToBigIntOrNumber(runtime , c_attachment.len));
|
|
177
|
+
// Factor this out as: cSliceToUInt8Array()
|
|
178
|
+
std::vector<uint8_t> id(c_attachment.id.ptr, c_attachment.id.ptr + c_attachment.id.len);
|
|
179
|
+
TypedArray<TypedArrayKind::Uint8Array> typedArray(runtime, id);
|
|
180
|
+
attachment.setProperty(runtime, "id", typedArray);
|
|
181
|
+
attachment.setProperty(runtime, "handle", cPointerToJSPointerObject(runtime, c_attachment.handle));
|
|
182
|
+
|
|
183
|
+
return 0;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
Function ditto_new_attachment_from_file(Runtime &runtime)
|
|
188
|
+
{
|
|
189
|
+
return Function::createFromHostFunction(runtime,
|
|
190
|
+
PropNameID::forAscii(runtime,
|
|
191
|
+
STRINGIFIED_FUNC_NAME()),
|
|
192
|
+
0,
|
|
193
|
+
[](Runtime &runtime,
|
|
194
|
+
const Value &thisValue,
|
|
195
|
+
const Value *arguments,
|
|
196
|
+
size_t count) -> Value
|
|
197
|
+
{
|
|
198
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
199
|
+
std::string source_path_str = jsTypedArrayToCString(runtime, arguments[1]);
|
|
200
|
+
std::string file_operation_str = arguments[2].toString(runtime).utf8(runtime);
|
|
201
|
+
|
|
202
|
+
AttachmentFileOperation_t file_operation;
|
|
203
|
+
if (file_operation_str == "Copy") {
|
|
204
|
+
file_operation = ATTACHMENT_FILE_OPERATION_COPY;
|
|
205
|
+
} else if (file_operation_str == "Move") {
|
|
206
|
+
file_operation = ATTACHMENT_FILE_OPERATION_MOVE;
|
|
207
|
+
} else {
|
|
208
|
+
throw "Invalid attachment file operation.";
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
CAttachment_t c_attachment;
|
|
212
|
+
uint32_t res = ::ditto_new_attachment_from_file(ditto, source_path_str.c_str(), file_operation, &c_attachment);
|
|
213
|
+
|
|
214
|
+
if (res != 0) {
|
|
215
|
+
// It failed, bubble up the error.
|
|
216
|
+
return static_cast<double>(res);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
Object attachment = arguments[3].getObject(runtime);
|
|
220
|
+
|
|
221
|
+
attachment.setProperty(runtime, "len", uint64ToBigIntOrNumber(runtime, c_attachment.len));
|
|
222
|
+
// Factor this out as: cSliceToUInt8Array()
|
|
223
|
+
std::vector<uint8_t> id(c_attachment.id.ptr, c_attachment.id.ptr + c_attachment.id.len);
|
|
224
|
+
TypedArray<TypedArrayKind::Uint8Array> typedArray(runtime, id);
|
|
225
|
+
attachment.setProperty(runtime, "id", typedArray);
|
|
226
|
+
attachment.setProperty(runtime, "handle", cPointerToJSPointerObject(runtime, c_attachment.handle));
|
|
227
|
+
|
|
228
|
+
return 0;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
Function ditto_resolve_attachment(Runtime &runtime)
|
|
233
|
+
{
|
|
234
|
+
return Function::createFromHostFunction(runtime,
|
|
235
|
+
PropNameID::forAscii(runtime,
|
|
236
|
+
STRINGIFIED_FUNC_NAME()),
|
|
237
|
+
0,
|
|
238
|
+
[](Runtime &runtime,
|
|
239
|
+
const Value &thisValue,
|
|
240
|
+
const Value *arguments,
|
|
241
|
+
size_t count) -> Value
|
|
242
|
+
{
|
|
243
|
+
CDitto_t *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
|
|
244
|
+
|
|
245
|
+
std::vector<uint8_t> id_vec = jsTypedArrayToCVector(runtime, arguments[1]);
|
|
246
|
+
slice_ref_uint8_t id = borrowVecAsRefSlice(id_vec);
|
|
247
|
+
|
|
248
|
+
Function onCompleteCallback = arguments[2].getObject(runtime).getFunction(runtime);
|
|
249
|
+
Function onProgressCallback = arguments[3].getObject(runtime).getFunction(runtime);
|
|
250
|
+
Function onDeleteCallback = arguments[4].getObject(runtime).getFunction(runtime);
|
|
251
|
+
|
|
252
|
+
AttachmentResolver resolver(std::move(onCompleteCallback),
|
|
253
|
+
std::move(onProgressCallback),
|
|
254
|
+
std::move(onDeleteCallback));
|
|
255
|
+
Arc<AttachmentResolver> wrappedStateFfiWrapper(std::move(resolver));
|
|
256
|
+
|
|
257
|
+
CancelTokenResult_t res = ::ditto_resolve_attachment(ditto,
|
|
258
|
+
id,
|
|
259
|
+
Arc<AttachmentResolver>::into_raw(std::move(wrappedStateFfiWrapper)),
|
|
260
|
+
nullptr,
|
|
261
|
+
nullptr,
|
|
262
|
+
v_on_complete,
|
|
263
|
+
v_on_progress,
|
|
264
|
+
v_on_deleted);
|
|
265
|
+
|
|
266
|
+
Object obj(runtime);
|
|
267
|
+
obj.setProperty(runtime, "status_code", static_cast<double>(res.status_code));
|
|
268
|
+
obj.setProperty(runtime, "cancel_token", int64ToBigIntOrNumber(runtime, res.cancel_token));
|
|
269
|
+
return obj;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|