@derec-alliance/react-native 0.0.5 → 0.0.6
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/README.md +9 -15
- package/android/CMakeLists.txt +1 -1
- package/android/src/main/jniLibs/arm64-v8a/libderec_library.a +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libderec_library.a +0 -0
- package/android/src/main/jniLibs/x86_64/libderec_library.a +0 -0
- package/cpp/Convert.cpp +125 -0
- package/cpp/Convert.h +35 -0
- package/cpp/DeRecInstaller.cpp +0 -12
- package/cpp/Primitives.cpp +159 -26
- package/cpp/Primitives.h +16 -0
- package/cpp/ProtocolHost.cpp +22 -47
- package/cpp/StoreCallbacks.cpp +97 -304
- package/cpp/UserSecretsJson.cpp +109 -0
- package/cpp/UserSecretsJson.h +28 -0
- package/cpp/derec_ffi.h +256 -120
- package/ios/DeRecFFI.xcframework/ios-arm64/Headers/derec_ffi.h +256 -120
- package/ios/DeRecFFI.xcframework/ios-arm64/libderec_library.a +0 -0
- package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/Headers/derec_ffi.h +256 -120
- package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/libderec_library.a +2 -2
- package/lib/errors.d.ts +7 -0
- package/lib/native.d.ts +5 -1
- package/lib/primitives.d.ts +18 -21
- package/lib/primitives.js +48 -25
- package/lib/protocol.d.ts +49 -97
- package/lib/protocol.js +71 -162
- package/lib/types.d.ts +207 -107
- package/lib/types.js +43 -13
- package/package.json +1 -1
- package/src/errors.ts +7 -0
- package/src/native.ts +2 -1
- package/src/primitives.ts +82 -25
- package/src/protocol.ts +79 -179
- package/src/types.ts +221 -96
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
|
|
3
|
+
|
|
4
|
+
#include "UserSecretsJson.h"
|
|
5
|
+
|
|
6
|
+
#include <string>
|
|
7
|
+
|
|
8
|
+
#include "Primitives.h"
|
|
9
|
+
|
|
10
|
+
using namespace facebook;
|
|
11
|
+
|
|
12
|
+
namespace derec {
|
|
13
|
+
|
|
14
|
+
// `UserSecrets` carries application-controlled `name` / `description`
|
|
15
|
+
// strings, so building or reading its wire JSON goes through the runtime's
|
|
16
|
+
// own `JSON.parse` / `JSON.stringify` rather than a hand-rolled scanner.
|
|
17
|
+
|
|
18
|
+
namespace {
|
|
19
|
+
|
|
20
|
+
std::vector<uint8_t> jsonStringifyToUtf8(jsi::Runtime& rt, const jsi::Value& value) {
|
|
21
|
+
jsi::Object json = rt.global().getPropertyAsObject(rt, "JSON");
|
|
22
|
+
jsi::Function stringify = json.getPropertyAsFunction(rt, "stringify");
|
|
23
|
+
jsi::Value result = stringify.call(rt, value);
|
|
24
|
+
std::string text = result.asString(rt).utf8(rt);
|
|
25
|
+
return std::vector<uint8_t>(text.begin(), text.end());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
jsi::Array bytesToNumberArray(jsi::Runtime& rt, ByteView view) {
|
|
29
|
+
jsi::Array arr(rt, view.len);
|
|
30
|
+
for (size_t i = 0; i < view.len; ++i) {
|
|
31
|
+
arr.setValueAtIndex(rt, i, jsi::Value(static_cast<int>(view.ptr[i])));
|
|
32
|
+
}
|
|
33
|
+
return arr;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
std::vector<uint8_t> numberArrayToBytes(jsi::Runtime& rt, jsi::Array arr) {
|
|
37
|
+
size_t n = arr.size(rt);
|
|
38
|
+
std::vector<uint8_t> out;
|
|
39
|
+
out.reserve(n);
|
|
40
|
+
for (size_t i = 0; i < n; ++i) {
|
|
41
|
+
out.push_back(static_cast<uint8_t>(arr.getValueAtIndex(rt, i).asNumber()));
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
} // namespace
|
|
47
|
+
|
|
48
|
+
jsi::Value jsonParseUtf8(jsi::Runtime& rt, const uint8_t* bytes, size_t len) {
|
|
49
|
+
jsi::Object json = rt.global().getPropertyAsObject(rt, "JSON");
|
|
50
|
+
jsi::Function parse = json.getPropertyAsFunction(rt, "parse");
|
|
51
|
+
std::string text(reinterpret_cast<const char*>(bytes), len);
|
|
52
|
+
return parse.call(rt, jsi::Value(rt, jsi::String::createFromUtf8(rt, text)));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Only the byte arrays inside `secrets` change representation (`Uint8Array`
|
|
56
|
+
// on the JS side, a JSON number array on the wire). Every record-level field
|
|
57
|
+
// (`version`, `description`, `author_replica_id`, ...) crosses verbatim, so
|
|
58
|
+
// whatever Rust writes is what the store hands back.
|
|
59
|
+
|
|
60
|
+
std::vector<uint8_t> userSecretsToWire(jsi::Runtime& rt, const jsi::Object& src) {
|
|
61
|
+
jsi::Object wire(rt);
|
|
62
|
+
jsi::Array names = src.getPropertyNames(rt);
|
|
63
|
+
size_t fieldCount = names.size(rt);
|
|
64
|
+
for (size_t i = 0; i < fieldCount; ++i) {
|
|
65
|
+
std::string name = names.getValueAtIndex(rt, i).asString(rt).utf8(rt);
|
|
66
|
+
if (name == "secrets") continue;
|
|
67
|
+
jsi::Value value = src.getProperty(rt, name.c_str());
|
|
68
|
+
if (!value.isUndefined()) {
|
|
69
|
+
wire.setProperty(rt, name.c_str(), value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
jsi::Array srcSecrets = src.getProperty(rt, "secrets").asObject(rt).asArray(rt);
|
|
73
|
+
size_t n = srcSecrets.size(rt);
|
|
74
|
+
jsi::Array wireSecrets(rt, n);
|
|
75
|
+
for (size_t i = 0; i < n; ++i) {
|
|
76
|
+
jsi::Object entry = srcSecrets.getValueAtIndex(rt, i).asObject(rt);
|
|
77
|
+
jsi::Object wireEntry(rt);
|
|
78
|
+
wireEntry.setProperty(rt, "id", jsi::Value(rt, bytesToNumberArray(rt, asBytes(rt, entry.getProperty(rt, "id")))));
|
|
79
|
+
wireEntry.setProperty(rt, "name", jsi::Value(rt, entry.getProperty(rt, "name")));
|
|
80
|
+
wireEntry.setProperty(
|
|
81
|
+
rt, "data", jsi::Value(rt, bytesToNumberArray(rt, asBytes(rt, entry.getProperty(rt, "data")))));
|
|
82
|
+
wireSecrets.setValueAtIndex(rt, i, jsi::Value(rt, wireEntry));
|
|
83
|
+
}
|
|
84
|
+
wire.setProperty(rt, "secrets", jsi::Value(rt, wireSecrets));
|
|
85
|
+
return jsonStringifyToUtf8(rt, jsi::Value(rt, wire));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
jsi::Value userSecretsFromWire(jsi::Runtime& rt, const uint8_t* ptr, size_t len) {
|
|
89
|
+
jsi::Object out = jsonParseUtf8(rt, ptr, len).asObject(rt);
|
|
90
|
+
jsi::Array srcSecrets = out.getProperty(rt, "secrets").asObject(rt).asArray(rt);
|
|
91
|
+
size_t n = srcSecrets.size(rt);
|
|
92
|
+
jsi::Array outSecrets(rt, n);
|
|
93
|
+
for (size_t i = 0; i < n; ++i) {
|
|
94
|
+
jsi::Object entry = srcSecrets.getValueAtIndex(rt, i).asObject(rt);
|
|
95
|
+
jsi::Object outEntry(rt);
|
|
96
|
+
std::vector<uint8_t> idBytes =
|
|
97
|
+
numberArrayToBytes(rt, entry.getProperty(rt, "id").asObject(rt).asArray(rt));
|
|
98
|
+
std::vector<uint8_t> dataBytes =
|
|
99
|
+
numberArrayToBytes(rt, entry.getProperty(rt, "data").asObject(rt).asArray(rt));
|
|
100
|
+
outEntry.setProperty(rt, "id", toUint8ArrayVal(rt, idBytes));
|
|
101
|
+
outEntry.setProperty(rt, "name", jsi::Value(rt, entry.getProperty(rt, "name")));
|
|
102
|
+
outEntry.setProperty(rt, "data", toUint8ArrayVal(rt, dataBytes));
|
|
103
|
+
outSecrets.setValueAtIndex(rt, i, jsi::Value(rt, outEntry));
|
|
104
|
+
}
|
|
105
|
+
out.setProperty(rt, "secrets", jsi::Value(rt, outSecrets));
|
|
106
|
+
return jsi::Value(rt, out);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
} // namespace derec
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <jsi/jsi.h>
|
|
7
|
+
|
|
8
|
+
#include <cstddef>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace derec {
|
|
13
|
+
|
|
14
|
+
/// `JSON.parse` of a UTF-8 buffer, through the runtime's own parser.
|
|
15
|
+
facebook::jsi::Value jsonParseUtf8(facebook::jsi::Runtime& rt, const uint8_t* bytes, size_t len);
|
|
16
|
+
|
|
17
|
+
/// A JS `UserSecrets` object as the `UserSecretsRecord` wire JSON
|
|
18
|
+
/// (`library/src/interop/ffi/protocol/stores.rs`).
|
|
19
|
+
std::vector<uint8_t> userSecretsToWire(facebook::jsi::Runtime& rt,
|
|
20
|
+
const facebook::jsi::Object& src);
|
|
21
|
+
|
|
22
|
+
/// `UserSecretsRecord` wire JSON as the JS `UserSecrets` shape
|
|
23
|
+
/// `src/types.ts` declares.
|
|
24
|
+
facebook::jsi::Value userSecretsFromWire(facebook::jsi::Runtime& rt,
|
|
25
|
+
const uint8_t* ptr,
|
|
26
|
+
size_t len);
|
|
27
|
+
|
|
28
|
+
} // namespace derec
|