simplex-chat 6.5.0-beta.4.1 → 6.5.0-beta.4.2
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 +1 -1
- package/cpp/simplex.cc +414 -0
- package/cpp/simplex.h +46 -0
- package/docs/Namespace.api.md +32 -0
- package/docs/Namespace.bot.md +20 -0
- package/docs/Namespace.core.md +48 -0
- package/docs/Namespace.util.md +25 -0
- package/docs/README.md +12 -0
- package/docs/api.Class.ChatApi.md +1602 -0
- package/docs/api.Class.ChatCommandError.md +205 -0
- package/docs/api.Enumeration.ConnReqType.md +27 -0
- package/docs/api.Interface.BotAddressSettings.md +60 -0
- package/docs/api.TypeAlias.EventSubscriberFunc.md +27 -0
- package/docs/api.TypeAlias.EventSubscribers.md +11 -0
- package/docs/api.Variable.defaultBotAddressSettings.md +11 -0
- package/docs/bot.Function.run.md +21 -0
- package/docs/bot.Interface.BotConfig.md +75 -0
- package/docs/bot.Interface.BotDbOpts.md +33 -0
- package/docs/bot.Interface.BotOptions.md +81 -0
- package/docs/core.Class.ChatAPIError.md +205 -0
- package/docs/core.Class.ChatInitError.md +205 -0
- package/docs/core.DBMigrationError.Interface.ErrorMigration.md +41 -0
- package/docs/core.DBMigrationError.Interface.ErrorNotADatabase.md +33 -0
- package/docs/core.DBMigrationError.Interface.ErrorSQL.md +41 -0
- package/docs/core.DBMigrationError.Interface.InvalidConfirmation.md +25 -0
- package/docs/core.DBMigrationError.TypeAlias.Tag.md +11 -0
- package/docs/core.Enumeration.MigrationConfirmation.md +43 -0
- package/docs/core.Function.chatCloseStore.md +23 -0
- package/docs/core.Function.chatDecryptFile.md +31 -0
- package/docs/core.Function.chatEncryptFile.md +31 -0
- package/docs/core.Function.chatMigrateInit.md +31 -0
- package/docs/core.Function.chatReadFile.md +27 -0
- package/docs/core.Function.chatRecvMsgWait.md +27 -0
- package/docs/core.Function.chatSendCmd.md +27 -0
- package/docs/core.Function.chatWriteFile.md +31 -0
- package/docs/core.Interface.APIResult.md +31 -0
- package/docs/core.Interface.CryptoArgs.md +27 -0
- package/docs/core.Interface.UpMigration.md +25 -0
- package/docs/core.MTRError.Interface.MTREDifferent.md +33 -0
- package/docs/core.MTRError.Interface.MTRENoDown.md +33 -0
- package/docs/core.MTRError.TypeAlias.Tag.md +11 -0
- package/docs/core.MigrationError.Interface.MEDowngrade.md +33 -0
- package/docs/core.MigrationError.Interface.MEUpgrade.md +33 -0
- package/docs/core.MigrationError.Interface.MigrationError.md +33 -0
- package/docs/core.MigrationError.TypeAlias.Tag.md +11 -0
- package/docs/core.Namespace.DBMigrationError.md +18 -0
- package/docs/core.Namespace.MTRError.md +16 -0
- package/docs/core.Namespace.MigrationError.md +17 -0
- package/docs/core.TypeAlias.DBMigrationError.md +11 -0
- package/docs/core.TypeAlias.MTRError.md +11 -0
- package/docs/core.TypeAlias.MigrationError.md +11 -0
- package/docs/util.Function.botAddressSettings.md +21 -0
- package/docs/util.Function.chatInfoName.md +21 -0
- package/docs/util.Function.chatInfoRef.md +21 -0
- package/docs/util.Function.ciBotCommand.md +21 -0
- package/docs/util.Function.ciContentText.md +21 -0
- package/docs/util.Function.contactAddressStr.md +21 -0
- package/docs/util.Function.fromLocalProfile.md +21 -0
- package/docs/util.Function.reactionText.md +21 -0
- package/docs/util.Function.senderName.md +25 -0
- package/docs/util.Interface.BotCommand.md +25 -0
- package/examples/squaring-bot-readme.js +17 -0
- package/examples/squaring-bot.ts +42 -0
- package/package.json +7 -3
- package/tsconfig.json +23 -0
package/README.md
CHANGED
package/cpp/simplex.cc
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <sstream>
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <functional>
|
|
5
|
+
#include <cstdlib>
|
|
6
|
+
#include "simplex.h"
|
|
7
|
+
|
|
8
|
+
namespace simplex {
|
|
9
|
+
|
|
10
|
+
using namespace Napi;
|
|
11
|
+
|
|
12
|
+
void haskell_init() {
|
|
13
|
+
int argc = 6;
|
|
14
|
+
const char *argv[] = {
|
|
15
|
+
"simplex",
|
|
16
|
+
"+RTS", // requires `hs_init_with_rtsopts`
|
|
17
|
+
"-A64m", // chunk size for new allocations
|
|
18
|
+
"-H64m", // initial heap size
|
|
19
|
+
"-xn", // non-moving GC
|
|
20
|
+
"--install-signal-handlers=no",
|
|
21
|
+
nullptr};
|
|
22
|
+
char **pargv = const_cast<char **>(argv);
|
|
23
|
+
hs_init_with_rtsopts(&argc, &pargv);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class ResultAsyncWorker : public AsyncWorker {
|
|
27
|
+
public:
|
|
28
|
+
using ExecuteFn = std::function<void(ResultAsyncWorker*)>;
|
|
29
|
+
using ResultProcessor = std::function<void(ResultAsyncWorker*, Napi::Env)>;
|
|
30
|
+
|
|
31
|
+
ResultAsyncWorker(Function& callback, ExecuteFn execute_fn, ResultProcessor result_processor = nullptr)
|
|
32
|
+
: AsyncWorker(callback), execute_fn_(std::move(execute_fn)), result_processor_(std::move(result_processor)) {}
|
|
33
|
+
|
|
34
|
+
void Execute() override {
|
|
35
|
+
execute_fn_(this);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void OnOK() override {
|
|
39
|
+
HandleScope scope(Env());
|
|
40
|
+
if (result_processor_) {
|
|
41
|
+
result_processor_(this, Env());
|
|
42
|
+
} else {
|
|
43
|
+
Callback().Call({Env().Null(), String::New(Env(), result_)});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void OnError(const Error& e) override {
|
|
48
|
+
HandleScope scope(Env());
|
|
49
|
+
Callback().Call({e.Value(), Env().Undefined()});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
void SetResult(std::string result) {
|
|
53
|
+
result_ = std::move(result);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
void SetWorkerError(const std::string& msg) {
|
|
57
|
+
SetError(msg);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const std::string& GetStringResult() const {
|
|
61
|
+
return result_;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void SetCtrl(uintptr_t ctrl) {
|
|
65
|
+
ctrl_ = ctrl;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
uintptr_t GetCtrl() const {
|
|
69
|
+
return ctrl_;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected:
|
|
73
|
+
std::string result_;
|
|
74
|
+
uintptr_t ctrl_ = 0;
|
|
75
|
+
|
|
76
|
+
private:
|
|
77
|
+
ExecuteFn execute_fn_;
|
|
78
|
+
ResultProcessor result_processor_;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
class BinaryAsyncWorker : public AsyncWorker {
|
|
82
|
+
public:
|
|
83
|
+
using ExecuteFn = std::function<void(BinaryAsyncWorker*)>;
|
|
84
|
+
|
|
85
|
+
BinaryAsyncWorker(Function& callback, ExecuteFn execute_fn)
|
|
86
|
+
: AsyncWorker(callback), execute_fn_(std::move(execute_fn)) {}
|
|
87
|
+
|
|
88
|
+
void Execute() override {
|
|
89
|
+
execute_fn_(this);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
void OnOK() override {
|
|
93
|
+
HandleScope scope(Env());
|
|
94
|
+
if (original_buf == nullptr || binary_len == 0) {
|
|
95
|
+
Callback().Call({Env().Null(), Env().Undefined()});
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
char* data_ptr = original_buf + 5;
|
|
99
|
+
auto finalizer = [](Napi::Env env, char* finalize_data, char* orig) {
|
|
100
|
+
free(orig);
|
|
101
|
+
};
|
|
102
|
+
Napi::Buffer<char> buffer = Napi::Buffer<char>::New(Env(), data_ptr, binary_len, finalizer, original_buf);
|
|
103
|
+
Callback().Call({Env().Null(), buffer});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void OnError(const Error& e) override {
|
|
107
|
+
HandleScope scope(Env());
|
|
108
|
+
Callback().Call({e.Value(), Env().Undefined()});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
void SetWorkerError(const std::string& msg) {
|
|
112
|
+
SetError(msg);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
char* original_buf = nullptr;
|
|
116
|
+
size_t binary_len = 0;
|
|
117
|
+
|
|
118
|
+
private:
|
|
119
|
+
ExecuteFn execute_fn_;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Helper for converting chat_ctrl pointer to BigInt
|
|
123
|
+
Napi::BigInt ToChatCtrlBigInt(Napi::Env env, uintptr_t ctrl) {
|
|
124
|
+
return Napi::BigInt::New(env, static_cast<uint64_t>(ctrl));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Helper for converting BigInt to chat_ctrl pointer
|
|
128
|
+
chat_ctrl FromChatCtrlBigInt(const Napi::Value& value) {
|
|
129
|
+
Napi::Env env = value.Env();
|
|
130
|
+
if (!value.IsBigInt()) {
|
|
131
|
+
Napi::TypeError::New(env, "Expected BigInt for ctrl").ThrowAsJavaScriptException();
|
|
132
|
+
return nullptr;
|
|
133
|
+
}
|
|
134
|
+
Napi::BigInt big = value.As<Napi::BigInt>();
|
|
135
|
+
bool lossless;
|
|
136
|
+
uint64_t val = big.Uint64Value(&lossless);
|
|
137
|
+
if (!lossless) {
|
|
138
|
+
Napi::TypeError::New(env, "BigInt too large for ctrl").ThrowAsJavaScriptException();
|
|
139
|
+
return nullptr;
|
|
140
|
+
}
|
|
141
|
+
return reinterpret_cast<chat_ctrl>(val);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Helper for handling common C result patterns (no empty check)
|
|
145
|
+
void HandleCResult(ResultAsyncWorker* worker, char* c_res, const std::string& func_name) {
|
|
146
|
+
if (c_res == nullptr) {
|
|
147
|
+
worker->SetWorkerError(func_name + " failed");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
std::string res = c_res;
|
|
151
|
+
free(c_res);
|
|
152
|
+
worker->SetResult(res);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
Napi::Promise CreatePromiseAndCallback(Env env, Function& cb_out) {
|
|
156
|
+
Promise::Deferred deferred = Promise::Deferred::New(env);
|
|
157
|
+
cb_out = Function::New(env, [deferred](const CallbackInfo& args) {
|
|
158
|
+
if (!args[0].IsNull() && !args[0].IsUndefined()) {
|
|
159
|
+
deferred.Reject(args[0]);
|
|
160
|
+
} else {
|
|
161
|
+
deferred.Resolve(args[1]);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return deferred.Promise();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Common result processors
|
|
168
|
+
ResultAsyncWorker::ResultProcessor MigrateResultProcessor() {
|
|
169
|
+
return [](ResultAsyncWorker* worker, Napi::Env env) {
|
|
170
|
+
Napi::Array arr = Napi::Array::New(env, 2);
|
|
171
|
+
arr.Set(0u, ToChatCtrlBigInt(env, worker->GetCtrl()));
|
|
172
|
+
arr.Set(1u, Napi::String::New(env, worker->GetStringResult()));
|
|
173
|
+
worker->Callback().Call({env.Null(), arr});
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Refactored functions using common patterns
|
|
178
|
+
|
|
179
|
+
Value ChatMigrateInit(const CallbackInfo& args) {
|
|
180
|
+
Env env = args.Env();
|
|
181
|
+
if (args.Length() < 3 || !args[0].IsString() || !args[1].IsString() || !args[2].IsString()) {
|
|
182
|
+
TypeError::New(env, "Expected three string arguments").ThrowAsJavaScriptException();
|
|
183
|
+
return env.Undefined();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
std::string path = args[0].As<String>().Utf8Value();
|
|
187
|
+
std::string key = args[1].As<String>().Utf8Value();
|
|
188
|
+
std::string confirm = args[2].As<String>().Utf8Value();
|
|
189
|
+
|
|
190
|
+
Function cb;
|
|
191
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
192
|
+
|
|
193
|
+
auto execute_fn = [path, key, confirm](ResultAsyncWorker* worker) {
|
|
194
|
+
chat_ctrl ctrl = nullptr;
|
|
195
|
+
char* c_res = chat_migrate_init(path.c_str(), key.c_str(), confirm.c_str(), &ctrl);
|
|
196
|
+
worker->SetCtrl(reinterpret_cast<uintptr_t>(ctrl));
|
|
197
|
+
HandleCResult(worker, c_res, "chat_migrate_init");
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn), MigrateResultProcessor());
|
|
201
|
+
worker->Queue();
|
|
202
|
+
|
|
203
|
+
return promise;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
Value ChatCloseStore(const CallbackInfo& args) {
|
|
207
|
+
Env env = args.Env();
|
|
208
|
+
if (args.Length() < 1 || !args[0].IsBigInt()) {
|
|
209
|
+
TypeError::New(env, "Expected bigint (ctrl)").ThrowAsJavaScriptException();
|
|
210
|
+
return env.Undefined();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
chat_ctrl ctrl = FromChatCtrlBigInt(args[0]);
|
|
214
|
+
|
|
215
|
+
Function cb;
|
|
216
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
217
|
+
|
|
218
|
+
auto execute_fn = [ctrl](ResultAsyncWorker* worker) {
|
|
219
|
+
char* c_res = chat_close_store(ctrl);
|
|
220
|
+
HandleCResult(worker, c_res, "chat_close_store");
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
224
|
+
worker->Queue();
|
|
225
|
+
|
|
226
|
+
return promise;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
Value ChatSendCmd(const CallbackInfo& args) {
|
|
230
|
+
Env env = args.Env();
|
|
231
|
+
if (args.Length() < 2 || !args[0].IsBigInt() || !args[1].IsString()) {
|
|
232
|
+
TypeError::New(env, "Expected bigint (ctrl) and string (cmd)").ThrowAsJavaScriptException();
|
|
233
|
+
return env.Undefined();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
chat_ctrl ctrl = FromChatCtrlBigInt(args[0]);
|
|
237
|
+
std::string cmd = args[1].As<String>().Utf8Value();
|
|
238
|
+
|
|
239
|
+
Function cb;
|
|
240
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
241
|
+
|
|
242
|
+
auto execute_fn = [ctrl, cmd](ResultAsyncWorker* worker) {
|
|
243
|
+
char* c_res = chat_send_cmd(ctrl, cmd.c_str());
|
|
244
|
+
HandleCResult(worker, c_res, "chat_send_cmd");
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
248
|
+
worker->Queue();
|
|
249
|
+
|
|
250
|
+
return promise;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
Value ChatRecvMsgWait(const CallbackInfo& args) {
|
|
254
|
+
Env env = args.Env();
|
|
255
|
+
if (args.Length() < 2 || !args[0].IsBigInt() || !args[1].IsNumber()) {
|
|
256
|
+
TypeError::New(env, "Expected bigint (ctrl), number (wait)").ThrowAsJavaScriptException();
|
|
257
|
+
return env.Undefined();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
chat_ctrl ctrl = FromChatCtrlBigInt(args[0]);
|
|
261
|
+
int wait = static_cast<int>(args[1].As<Number>().Int32Value());
|
|
262
|
+
|
|
263
|
+
Function cb;
|
|
264
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
265
|
+
|
|
266
|
+
auto execute_fn = [ctrl, wait](ResultAsyncWorker* worker) {
|
|
267
|
+
char* c_res = chat_recv_msg_wait(ctrl, wait);
|
|
268
|
+
HandleCResult(worker, c_res, "chat_recv_msg_wait");
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
272
|
+
worker->Queue();
|
|
273
|
+
|
|
274
|
+
return promise;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
Value ChatWriteFile(const CallbackInfo& args) {
|
|
278
|
+
Env env = args.Env();
|
|
279
|
+
if (args.Length() < 3 || !args[0].IsBigInt() || !args[1].IsString() || !args[2].IsArrayBuffer()) {
|
|
280
|
+
TypeError::New(env, "Expected bigint (ctrl), string (path), ArrayBuffer").ThrowAsJavaScriptException();
|
|
281
|
+
return env.Undefined();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
chat_ctrl ctrl = FromChatCtrlBigInt(args[0]);
|
|
285
|
+
std::string path = args[1].As<String>().Utf8Value();
|
|
286
|
+
ArrayBuffer ab = args[2].As<ArrayBuffer>();
|
|
287
|
+
char* data = static_cast<char*>(ab.Data());
|
|
288
|
+
size_t len = ab.ByteLength();
|
|
289
|
+
|
|
290
|
+
Function cb;
|
|
291
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
292
|
+
|
|
293
|
+
auto execute_fn = [ctrl, path, ab, data, len](ResultAsyncWorker* worker) {
|
|
294
|
+
(void)ab; // to keep ArrayBuffer alive
|
|
295
|
+
char* c_res = chat_write_file(ctrl, path.c_str(), data, static_cast<int>(len));
|
|
296
|
+
HandleCResult(worker, c_res, "chat_write_file");
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
300
|
+
worker->Queue();
|
|
301
|
+
|
|
302
|
+
return promise;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
Value ChatReadFile(const CallbackInfo& args) {
|
|
306
|
+
Env env = args.Env();
|
|
307
|
+
if (args.Length() < 3 || !args[0].IsString() || !args[1].IsString() || !args[2].IsString()) {
|
|
308
|
+
TypeError::New(env, "Expected three strings (path, key, nonce)").ThrowAsJavaScriptException();
|
|
309
|
+
return env.Undefined();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
std::string path = args[0].As<String>().Utf8Value();
|
|
313
|
+
std::string key = args[1].As<String>().Utf8Value();
|
|
314
|
+
std::string nonce = args[2].As<String>().Utf8Value();
|
|
315
|
+
|
|
316
|
+
Function cb;
|
|
317
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
318
|
+
|
|
319
|
+
auto execute_fn = [path, key, nonce](BinaryAsyncWorker* worker) {
|
|
320
|
+
char* buf = chat_read_file(path.c_str(), key.c_str(), nonce.c_str());
|
|
321
|
+
if (buf == nullptr) {
|
|
322
|
+
worker->SetWorkerError("chat_read_file failed");
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
char status = buf[0];
|
|
326
|
+
if (status == 1) {
|
|
327
|
+
std::string err = buf + 1;
|
|
328
|
+
free(buf);
|
|
329
|
+
worker->SetWorkerError(err);
|
|
330
|
+
return;
|
|
331
|
+
} else if (status == 0) {
|
|
332
|
+
uint32_t len = *(uint32_t*)(buf + 1);
|
|
333
|
+
worker->original_buf = buf;
|
|
334
|
+
worker->binary_len = len;
|
|
335
|
+
} else {
|
|
336
|
+
free(buf);
|
|
337
|
+
worker->SetWorkerError("Unexpected status from chat_read_file");
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
BinaryAsyncWorker* worker = new BinaryAsyncWorker(cb, std::move(execute_fn));
|
|
343
|
+
worker->Queue();
|
|
344
|
+
|
|
345
|
+
return promise;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
Value ChatEncryptFile(const CallbackInfo& args) {
|
|
349
|
+
Env env = args.Env();
|
|
350
|
+
if (args.Length() < 3 || !args[0].IsBigInt() || !args[1].IsString() || !args[2].IsString()) {
|
|
351
|
+
TypeError::New(env, "Expected bigint (ctrl), two strings (fromPath, toPath)").ThrowAsJavaScriptException();
|
|
352
|
+
return env.Undefined();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
chat_ctrl ctrl = FromChatCtrlBigInt(args[0]);
|
|
356
|
+
std::string fromPath = args[1].As<String>().Utf8Value();
|
|
357
|
+
std::string toPath = args[2].As<String>().Utf8Value();
|
|
358
|
+
|
|
359
|
+
Function cb;
|
|
360
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
361
|
+
|
|
362
|
+
auto execute_fn = [ctrl, fromPath, toPath](ResultAsyncWorker* worker) {
|
|
363
|
+
char* c_res = chat_encrypt_file(ctrl, fromPath.c_str(), toPath.c_str());
|
|
364
|
+
HandleCResult(worker, c_res, "chat_encrypt_file");
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
368
|
+
worker->Queue();
|
|
369
|
+
|
|
370
|
+
return promise;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
Value ChatDecryptFile(const CallbackInfo& args) {
|
|
374
|
+
Env env = args.Env();
|
|
375
|
+
if (args.Length() < 4 || !args[0].IsString() || !args[1].IsString() || !args[2].IsString() || !args[3].IsString()) {
|
|
376
|
+
TypeError::New(env, "Expected four strings (fromPath, key, nonce, toPath)").ThrowAsJavaScriptException();
|
|
377
|
+
return env.Undefined();
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
std::string fromPath = args[0].As<String>().Utf8Value();
|
|
381
|
+
std::string key = args[1].As<String>().Utf8Value();
|
|
382
|
+
std::string nonce = args[2].As<String>().Utf8Value();
|
|
383
|
+
std::string toPath = args[3].As<String>().Utf8Value();
|
|
384
|
+
|
|
385
|
+
Function cb;
|
|
386
|
+
Promise promise = CreatePromiseAndCallback(env, cb);
|
|
387
|
+
|
|
388
|
+
auto execute_fn = [fromPath, key, nonce, toPath](ResultAsyncWorker* worker) {
|
|
389
|
+
char* c_res = chat_decrypt_file(fromPath.c_str(), key.c_str(), nonce.c_str(), toPath.c_str());
|
|
390
|
+
HandleCResult(worker, c_res, "chat_decrypt_file");
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
ResultAsyncWorker* worker = new ResultAsyncWorker(cb, std::move(execute_fn));
|
|
394
|
+
worker->Queue();
|
|
395
|
+
|
|
396
|
+
return promise;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
Object Init(Env env, Object exports) {
|
|
400
|
+
haskell_init();
|
|
401
|
+
exports.Set("chat_migrate_init", Function::New(env, ChatMigrateInit));
|
|
402
|
+
exports.Set("chat_close_store", Function::New(env, ChatCloseStore));
|
|
403
|
+
exports.Set("chat_send_cmd", Function::New(env, ChatSendCmd));
|
|
404
|
+
exports.Set("chat_recv_msg_wait", Function::New(env, ChatRecvMsgWait));
|
|
405
|
+
exports.Set("chat_write_file", Function::New(env, ChatWriteFile));
|
|
406
|
+
exports.Set("chat_read_file", Function::New(env, ChatReadFile));
|
|
407
|
+
exports.Set("chat_encrypt_file", Function::New(env, ChatEncryptFile));
|
|
408
|
+
exports.Set("chat_decrypt_file", Function::New(env, ChatDecryptFile));
|
|
409
|
+
return exports;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
NODE_API_MODULE(simplex, Init)
|
|
413
|
+
|
|
414
|
+
}
|
package/cpp/simplex.h
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// simplex.h
|
|
3
|
+
// SimpleX
|
|
4
|
+
//
|
|
5
|
+
// Created by Evgeny on 30/05/2022.
|
|
6
|
+
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
#ifndef SimpleX_h
|
|
10
|
+
#define SimpleX_h
|
|
11
|
+
|
|
12
|
+
extern "C" void hs_init(int argc, char **argv[]);
|
|
13
|
+
extern "C" void hs_init_with_rtsopts(int * argc, char **argv[]);
|
|
14
|
+
|
|
15
|
+
typedef long* chat_ctrl;
|
|
16
|
+
|
|
17
|
+
// the last parameter is used to return the pointer to chat controller
|
|
18
|
+
extern "C" char *chat_migrate_init(const char *path, const char *key, const char *confirm, chat_ctrl *ctrl);
|
|
19
|
+
extern "C" char *chat_close_store(chat_ctrl ctrl);
|
|
20
|
+
extern "C" char *chat_reopen_store(chat_ctrl ctrl);
|
|
21
|
+
extern "C" char *chat_send_cmd(chat_ctrl ctrl, const char *cmd);
|
|
22
|
+
extern "C" char *chat_recv_msg_wait(chat_ctrl ctrl, const int wait);
|
|
23
|
+
extern "C" char *chat_parse_markdown(const char *str);
|
|
24
|
+
extern "C" char *chat_parse_server(const char *str);
|
|
25
|
+
extern "C" char *chat_password_hash(const char *pwd, const char *salt);
|
|
26
|
+
extern "C" char *chat_valid_name(const char *name);
|
|
27
|
+
extern "C" int chat_json_length(const char *str);
|
|
28
|
+
extern "C" char *chat_encrypt_media(chat_ctrl ctrl, const char *key, const char *frame, const int len);
|
|
29
|
+
extern "C" char *chat_decrypt_media(const char *key, const char *frame, const int len);
|
|
30
|
+
|
|
31
|
+
// chat_write_file returns null-terminated string with JSON of WriteFileResult
|
|
32
|
+
extern "C" char *chat_write_file(chat_ctrl ctrl, const char *path, const char *data, const int len);
|
|
33
|
+
|
|
34
|
+
// chat_read_file returns a buffer with:
|
|
35
|
+
// result status (1 byte), then if
|
|
36
|
+
// status == 0 (success): buffer length (uint32, 4 bytes), buffer of specified length.
|
|
37
|
+
// status == 1 (error): null-terminated error message string.
|
|
38
|
+
extern "C" char *chat_read_file(const char *path, const char *key, const char *nonce);
|
|
39
|
+
|
|
40
|
+
// chat_encrypt_file returns null-terminated string with JSON of WriteFileResult
|
|
41
|
+
extern "C" char *chat_encrypt_file(chat_ctrl ctrl, const char *fromPath, const char *toPath);
|
|
42
|
+
|
|
43
|
+
// chat_decrypt_file returns null-terminated string with the error message
|
|
44
|
+
extern "C" char *chat_decrypt_file(const char *fromPath, const char *key, const char *nonce, const char *toPath);
|
|
45
|
+
|
|
46
|
+
#endif /* simplex_h */
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[**simplex-chat**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[simplex-chat](README.md) / api
|
|
6
|
+
|
|
7
|
+
# api
|
|
8
|
+
|
|
9
|
+
An API to send chat commands and receive chat events to/from chat core.
|
|
10
|
+
You need to use it in bot event handlers, and for any other use cases.
|
|
11
|
+
|
|
12
|
+
## Enumerations
|
|
13
|
+
|
|
14
|
+
- [ConnReqType](api.Enumeration.ConnReqType.md)
|
|
15
|
+
|
|
16
|
+
## Classes
|
|
17
|
+
|
|
18
|
+
- [ChatApi](api.Class.ChatApi.md)
|
|
19
|
+
- [ChatCommandError](api.Class.ChatCommandError.md)
|
|
20
|
+
|
|
21
|
+
## Interfaces
|
|
22
|
+
|
|
23
|
+
- [BotAddressSettings](api.Interface.BotAddressSettings.md)
|
|
24
|
+
|
|
25
|
+
## Type Aliases
|
|
26
|
+
|
|
27
|
+
- [EventSubscriberFunc](api.TypeAlias.EventSubscriberFunc.md)
|
|
28
|
+
- [EventSubscribers](api.TypeAlias.EventSubscribers.md)
|
|
29
|
+
|
|
30
|
+
## Variables
|
|
31
|
+
|
|
32
|
+
- [defaultBotAddressSettings](api.Variable.defaultBotAddressSettings.md)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[**simplex-chat**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[simplex-chat](README.md) / bot
|
|
6
|
+
|
|
7
|
+
# bot
|
|
8
|
+
|
|
9
|
+
A simple declarative API to run a chat-bot with a single function call.
|
|
10
|
+
It automates creating and updating of the bot profile, address and bot commands shown in the app UI.
|
|
11
|
+
|
|
12
|
+
## Interfaces
|
|
13
|
+
|
|
14
|
+
- [BotConfig](bot.Interface.BotConfig.md)
|
|
15
|
+
- [BotDbOpts](bot.Interface.BotDbOpts.md)
|
|
16
|
+
- [BotOptions](bot.Interface.BotOptions.md)
|
|
17
|
+
|
|
18
|
+
## Functions
|
|
19
|
+
|
|
20
|
+
- [run](bot.Function.run.md)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[**simplex-chat**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[simplex-chat](README.md) / core
|
|
6
|
+
|
|
7
|
+
# core
|
|
8
|
+
|
|
9
|
+
A low level API to the core library - the same that is used in desktop clients.
|
|
10
|
+
You are unlikely to ever need to use this module directly.
|
|
11
|
+
|
|
12
|
+
## Namespaces
|
|
13
|
+
|
|
14
|
+
- [DBMigrationError](core.Namespace.DBMigrationError.md)
|
|
15
|
+
- [MigrationError](core.Namespace.MigrationError.md)
|
|
16
|
+
- [MTRError](core.Namespace.MTRError.md)
|
|
17
|
+
|
|
18
|
+
## Enumerations
|
|
19
|
+
|
|
20
|
+
- [MigrationConfirmation](core.Enumeration.MigrationConfirmation.md)
|
|
21
|
+
|
|
22
|
+
## Classes
|
|
23
|
+
|
|
24
|
+
- [ChatAPIError](core.Class.ChatAPIError.md)
|
|
25
|
+
- [ChatInitError](core.Class.ChatInitError.md)
|
|
26
|
+
|
|
27
|
+
## Interfaces
|
|
28
|
+
|
|
29
|
+
- [APIResult](core.Interface.APIResult.md)
|
|
30
|
+
- [CryptoArgs](core.Interface.CryptoArgs.md)
|
|
31
|
+
- [UpMigration](core.Interface.UpMigration.md)
|
|
32
|
+
|
|
33
|
+
## Type Aliases
|
|
34
|
+
|
|
35
|
+
- [DBMigrationError](core.TypeAlias.DBMigrationError.md)
|
|
36
|
+
- [MigrationError](core.TypeAlias.MigrationError.md)
|
|
37
|
+
- [MTRError](core.TypeAlias.MTRError.md)
|
|
38
|
+
|
|
39
|
+
## Functions
|
|
40
|
+
|
|
41
|
+
- [chatCloseStore](core.Function.chatCloseStore.md)
|
|
42
|
+
- [chatDecryptFile](core.Function.chatDecryptFile.md)
|
|
43
|
+
- [chatEncryptFile](core.Function.chatEncryptFile.md)
|
|
44
|
+
- [chatMigrateInit](core.Function.chatMigrateInit.md)
|
|
45
|
+
- [chatReadFile](core.Function.chatReadFile.md)
|
|
46
|
+
- [chatRecvMsgWait](core.Function.chatRecvMsgWait.md)
|
|
47
|
+
- [chatSendCmd](core.Function.chatSendCmd.md)
|
|
48
|
+
- [chatWriteFile](core.Function.chatWriteFile.md)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[**simplex-chat**](README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[simplex-chat](README.md) / util
|
|
6
|
+
|
|
7
|
+
# util
|
|
8
|
+
|
|
9
|
+
Useful functions for chat events and types.
|
|
10
|
+
|
|
11
|
+
## Interfaces
|
|
12
|
+
|
|
13
|
+
- [BotCommand](util.Interface.BotCommand.md)
|
|
14
|
+
|
|
15
|
+
## Functions
|
|
16
|
+
|
|
17
|
+
- [botAddressSettings](util.Function.botAddressSettings.md)
|
|
18
|
+
- [chatInfoName](util.Function.chatInfoName.md)
|
|
19
|
+
- [chatInfoRef](util.Function.chatInfoRef.md)
|
|
20
|
+
- [ciBotCommand](util.Function.ciBotCommand.md)
|
|
21
|
+
- [ciContentText](util.Function.ciContentText.md)
|
|
22
|
+
- [contactAddressStr](util.Function.contactAddressStr.md)
|
|
23
|
+
- [fromLocalProfile](util.Function.fromLocalProfile.md)
|
|
24
|
+
- [reactionText](util.Function.reactionText.md)
|
|
25
|
+
- [senderName](util.Function.senderName.md)
|