2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.qjs.h"
|
|
|
|
|
2021-08-22 15:41:27 -04:00
|
|
|
#include "ssb.db.h"
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "ssb.h"
|
|
|
|
#include "task.h"
|
|
|
|
|
|
|
|
#include <malloc.h>
|
2021-09-06 13:50:38 -04:00
|
|
|
#include <sodium/crypto_hash_sha256.h>
|
|
|
|
#include <sodium/crypto_sign.h>
|
|
|
|
#include <string.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <uv.h>
|
|
|
|
|
|
|
|
#include "quickjs-libc.h"
|
|
|
|
|
|
|
|
static JSClassID _tf_ssb_classId;
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_whoami(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
char id[512];
|
|
|
|
if (tf_ssb_whoami(ssb, id, sizeof(id))) {
|
|
|
|
return JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_getMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
int64_t sequence = 0;
|
|
|
|
JS_ToInt64(context, &sequence, argv[1]);
|
|
|
|
int64_t timestamp = -1;
|
|
|
|
char* contents = NULL;
|
2021-08-22 15:41:27 -04:00
|
|
|
if (tf_ssb_db_get_message_by_author_and_sequence(ssb, id, sequence, NULL, 0, ×tamp, &contents)) {
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewObject(context);
|
|
|
|
JS_SetPropertyStr(context, result, "timestamp", JS_NewInt64(context, timestamp));
|
|
|
|
JS_SetPropertyStr(context, result, "content", JS_NewString(context, contents));
|
|
|
|
free(contents);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, id);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
2021-08-22 15:41:27 -04:00
|
|
|
if (tf_ssb_db_blob_get(ssb, id, &blob, &size)) {
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
|
|
|
free(blob);
|
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeCString(context, id);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
char id[512];
|
|
|
|
if (JS_IsString(argv[0])) {
|
|
|
|
const char* text = JS_ToCStringLen(context, &size, argv[0]);
|
2021-08-22 15:41:27 -04:00
|
|
|
if (tf_ssb_db_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id))) {
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
JS_FreeCString(context, text);
|
|
|
|
} else if ((blob = tf_try_get_array_buffer(context, &size, argv[0])) != 0) {
|
2021-08-22 15:41:27 -04:00
|
|
|
if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id))) {
|
2021-01-02 13:10:00 -05:00
|
|
|
result = JS_NewString(context, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:06:33 -05:00
|
|
|
static JSValue _tf_ssb_messageContentGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* id = JS_ToCString(context, argv[0]);
|
|
|
|
uint8_t* blob = NULL;
|
|
|
|
size_t size = 0;
|
2021-08-22 15:41:27 -04:00
|
|
|
if (tf_ssb_db_message_content_get(ssb, id, &blob, &size)) {
|
2021-01-09 18:06:33 -05:00
|
|
|
result = JS_NewArrayBufferCopy(context, blob, size);
|
|
|
|
free(blob);
|
|
|
|
}
|
2021-09-06 16:54:44 -04:00
|
|
|
JS_FreeCString(context, id);
|
2021-01-09 18:06:33 -05:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char** connections = tf_ssb_get_connection_ids(ssb);
|
|
|
|
if (connections) {
|
|
|
|
result = JS_NewArray(context);
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (const char** p = connections; *p; p++, i++) {
|
|
|
|
JS_SetPropertyUint32(context, result, i, JS_NewString(context, *p));
|
|
|
|
}
|
|
|
|
free(connections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
static void _check_call(JSContext* context, JSValue result)
|
|
|
|
{
|
|
|
|
if (JS_IsError(context, result))
|
|
|
|
{
|
|
|
|
const char* value = JS_ToCString(context, result);
|
|
|
|
printf("ERROR: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JSValue stack = JS_GetPropertyStr(context, result, "stack");
|
|
|
|
if (!JS_IsUndefined(stack)) {
|
|
|
|
const char* stack_str = JS_ToCString(context, stack);
|
|
|
|
printf("%s\n", stack_str);
|
|
|
|
JS_FreeCString(context, stack_str);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, stack);
|
|
|
|
}
|
|
|
|
else if (JS_IsException(result))
|
|
|
|
{
|
|
|
|
js_std_dump_error(context);
|
|
|
|
JSValue error = JS_GetException(context);
|
|
|
|
const char* value = JS_ToCString(context, error);
|
|
|
|
printf("Exception: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _sqlStream_callback_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue callback;
|
|
|
|
} sqlStream_callback_t;
|
|
|
|
|
|
|
|
static void _tf_ssb_sqlStream_callback(JSValue row, void* user_data) {
|
|
|
|
sqlStream_callback_t* info = user_data;
|
|
|
|
JSValue response = JS_Call(info->context, info->callback, JS_UNDEFINED, 1, &row);
|
2021-09-06 13:50:38 -04:00
|
|
|
_check_call(info->context, response);
|
|
|
|
if (tf_task_get(info->context))
|
|
|
|
{
|
|
|
|
tf_task_run_jobs(tf_task_get(info->context));
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
JS_FreeValue(info->context, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_sqlStream(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* query = JS_ToCString(context, argv[0]);
|
|
|
|
if (query) {
|
|
|
|
sqlStream_callback_t info = {
|
|
|
|
.context = context,
|
|
|
|
.callback = argv[2],
|
|
|
|
};
|
2021-08-22 15:41:27 -04:00
|
|
|
tf_ssb_db_visit_query(ssb, query, argv[1], _tf_ssb_sqlStream_callback, &info);
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeCString(context, query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_post(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
const char* post_text = JS_ToCString(context, argv[0]);
|
|
|
|
if (post_text) {
|
|
|
|
tf_ssb_append_post(ssb, post_text);
|
|
|
|
JS_FreeCString(context, post_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_appendMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
tf_ssb_append_message(ssb, argv[0]);
|
|
|
|
}
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
static JSValue _tf_ssb_storeMessage(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
char signature[crypto_sign_BYTES + 128];
|
|
|
|
char id[crypto_hash_sha256_BYTES * 2 + 1];
|
|
|
|
tf_ssb_calculate_message_id(context, argv[0], id, sizeof(id));
|
|
|
|
if (tf_ssb_verify_and_strip_signature(context, argv[0], signature, sizeof(signature))) {
|
|
|
|
tf_ssb_db_store_message(ssb, context, id, argv[0], signature);
|
|
|
|
} else {
|
|
|
|
printf("failed to verify message\n");
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _broadcasts_t
|
|
|
|
{
|
|
|
|
JSContext* context;
|
|
|
|
JSValue array;
|
|
|
|
int length;
|
|
|
|
} broadcasts_t;
|
|
|
|
|
|
|
|
static void _tf_ssb_broadcasts_visit(const struct sockaddr_in* addr, const uint8_t* pub, void* user_data)
|
|
|
|
{
|
|
|
|
broadcasts_t* broadcasts = user_data;
|
|
|
|
JSValue entry = JS_NewObject(broadcasts->context);
|
|
|
|
char address[256];
|
|
|
|
char pubkey[k_id_base64_len];
|
|
|
|
uv_ip4_name(addr, address, sizeof(address));
|
|
|
|
tf_ssb_id_bin_to_str(pubkey, sizeof(pubkey), pub);
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "address", JS_NewString(broadcasts->context, address));
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "port", JS_NewInt32(broadcasts->context, ntohs(addr->sin_port)));
|
|
|
|
JS_SetPropertyStr(broadcasts->context, entry, "pubkey", JS_NewString(broadcasts->context, pubkey));
|
|
|
|
JS_SetPropertyUint32(broadcasts->context, broadcasts->array, broadcasts->length++, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue result = JS_UNDEFINED;
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
result = JS_NewArray(context);
|
|
|
|
broadcasts_t broadcasts = {
|
|
|
|
.context = context,
|
|
|
|
.array = result,
|
|
|
|
.length = 0,
|
|
|
|
};
|
|
|
|
tf_ssb_visit_broadcasts(ssb, _tf_ssb_broadcasts_visit, &broadcasts);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue args = argv[0];
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (ssb) {
|
|
|
|
if (JS_IsString(args)) {
|
|
|
|
const char* address_str = JS_ToCString(context, args);
|
|
|
|
printf("Connecting to %s\n", address_str);
|
|
|
|
tf_ssb_connect_str(ssb, address_str);
|
|
|
|
JS_FreeCString(context, address_str);
|
|
|
|
} else {
|
|
|
|
JSValue address = JS_GetPropertyStr(context, args, "address");
|
|
|
|
JSValue port = JS_GetPropertyStr(context, args, "port");
|
|
|
|
JSValue pubkey = JS_GetPropertyStr(context, args, "pubkey");
|
|
|
|
const char* address_str = JS_ToCString(context, address);
|
|
|
|
int32_t port_int = 0;
|
|
|
|
JS_ToInt32(context, &port_int, port);
|
|
|
|
const char* pubkey_str = JS_ToCString(context, pubkey);
|
|
|
|
uint8_t pubkey_bin[k_id_bin_len];
|
|
|
|
printf("Connecting to %s:%d\n", address_str, port_int);
|
|
|
|
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
|
|
|
|
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin);
|
|
|
|
JS_FreeCString(context, pubkey_str);
|
|
|
|
JS_FreeCString(context, address_str);
|
|
|
|
JS_FreeValue(context, address);
|
|
|
|
JS_FreeValue(context, port);
|
|
|
|
JS_FreeValue(context, pubkey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_call_callback(tf_ssb_t* ssb, const char* name, void* user_data)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue ssbo = JS_GetPropertyStr(context, global, "ssb");
|
|
|
|
JSValue callback = JS_GetPropertyStr(context, ssbo, name);
|
2021-09-06 13:50:38 -04:00
|
|
|
if (JS_IsFunction(context, callback)) {
|
|
|
|
JSValue args = JS_UNDEFINED;
|
|
|
|
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 0, &args);
|
|
|
|
_check_call(context, response);
|
|
|
|
if (tf_task_get(context))
|
|
|
|
{
|
|
|
|
tf_task_run_jobs(tf_task_get(context));
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, response);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
JS_FreeValue(context, ssbo);
|
|
|
|
JS_FreeValue(context, global);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_broadcasts_changed(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
_tf_ssb_call_callback(ssb, "onBroadcastsChanged", user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
|
|
|
{
|
|
|
|
_tf_ssb_call_callback(ssb, "onConnectionsChanged", user_data);
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:50:38 -04:00
|
|
|
static JSValue _tf_ssb_rpc_send_json(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
|
|
|
JSValue message_val = JS_JSONStringify(context, argv[0], JS_NULL, JS_NULL);
|
|
|
|
size_t size;
|
|
|
|
const char* message = JS_ToCStringLen(context, &size, message_val);
|
|
|
|
|
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
|
|
|
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream,
|
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message,
|
|
|
|
size,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
JS_FreeValue(context, connection_val);
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeCString(context, message);
|
|
|
|
JS_FreeValue(context, message_val);
|
2021-09-06 13:50:38 -04:00
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_rpc_send_binary(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
JSValue connection_val = JS_GetPropertyStr(context, this_val, "connection");
|
|
|
|
tf_ssb_connection_t* connection = JS_GetOpaque(connection_val, tf_ssb_get_connection_class_id());
|
|
|
|
JSValue request_val = JS_GetPropertyStr(context, this_val, "request_number");
|
|
|
|
int32_t request_number;
|
|
|
|
JS_ToInt32(context, &request_number, request_val);
|
|
|
|
JS_FreeValue(context, request_val);
|
|
|
|
|
|
|
|
size_t size;
|
|
|
|
uint8_t* message = tf_try_get_array_buffer(context, &size, argv[0]);
|
|
|
|
if (message) {
|
|
|
|
tf_ssb_connection_rpc_send(
|
|
|
|
connection,
|
|
|
|
k_ssb_rpc_flag_binary | k_ssb_rpc_flag_stream,
|
|
|
|
-request_number,
|
|
|
|
(const uint8_t*)message,
|
|
|
|
size,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, connection_val);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _tf_ssb_on_rpc(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JSValue object = JS_NewObject(context);
|
|
|
|
JSValue connection_object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
|
|
|
JS_SetPropertyStr(context, object, "connection", connection_object);
|
|
|
|
JS_SetPropertyStr(context, object, "flags", JS_NewUint32(context, flags));
|
|
|
|
JS_SetPropertyStr(context, object, "request_number", JS_NewInt32(context, request_number));
|
|
|
|
JS_SetPropertyStr(context, object, "args", JS_GetPropertyStr(context, args, "args"));
|
|
|
|
JS_SetPropertyStr(context, object, "message", message && size ? JS_NewArrayBufferCopy(context, message, size) : args);
|
|
|
|
JS_SetPropertyStr(context, object, "send_json", JS_NewCFunction(context, _tf_ssb_rpc_send_json, "send_json", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "send_binary", JS_NewCFunction(context, _tf_ssb_rpc_send_binary, "send_binary", 1));
|
|
|
|
|
|
|
|
JSValue result = JS_Call(context, callback, JS_UNDEFINED, 1, &object);
|
|
|
|
_check_call(context, result);
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
JS_FreeValue(context, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_rpc_js_value_cleanup(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JS_FreeValue(tf_ssb_get_context(ssb), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_register_rpc(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (!JS_IsArray(context, argv[0]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 1 to be an array of strings.");
|
|
|
|
}
|
|
|
|
if (!JS_IsFunction(context, argv[1]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 2 to be a function.");
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue length_val = JS_GetPropertyStr(context, argv[0], "length");
|
|
|
|
int length = 0;
|
|
|
|
JS_ToInt32(context, &length, length_val);
|
|
|
|
|
|
|
|
enum { k_max_name_parts = 16 };
|
|
|
|
const char* name[k_max_name_parts + 1] = { 0 };
|
|
|
|
|
|
|
|
if (length >= k_max_name_parts)
|
|
|
|
{
|
|
|
|
return JS_ThrowInternalError(context, "Too many parts to RPC name.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
JSValue value = JS_GetPropertyUint32(context, argv[0], i);
|
|
|
|
name[i] = JS_ToCString(context, value);
|
|
|
|
JS_FreeValue(context, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_ssb_register_rpc(ssb, name, _tf_ssb_on_rpc, _tf_ssb_rpc_js_value_cleanup, JS_VALUE_GET_PTR(JS_DupValue(context, argv[1])));
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
JS_FreeCString(context, name[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_on_blob_want_added(tf_ssb_t* ssb, const char* id, void* user_data)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JSValue string = JS_NewString(context, id);
|
|
|
|
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &string);
|
|
|
|
_check_call(context, response);
|
|
|
|
JS_FreeValue(context, response);
|
|
|
|
JS_FreeValue(context, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_cleanup_value(tf_ssb_t* ssb, void* user_data)
|
|
|
|
{
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JS_FreeValue(tf_ssb_get_context(ssb), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_register_blob_want_added(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (!JS_IsFunction(context, argv[0]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 1 to be a function.");
|
|
|
|
}
|
|
|
|
tf_ssb_register_blob_want_added(ssb, _tf_ssb_on_blob_want_added, _tf_ssb_cleanup_value, JS_VALUE_GET_PTR(JS_DupValue(context, argv[0])));
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_ssb_rpc_on_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data)
|
|
|
|
{
|
|
|
|
JSContext* context = tf_ssb_get_context(ssb);
|
|
|
|
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
|
|
|
|
JSValue response = JS_UNDEFINED;
|
|
|
|
switch (change)
|
|
|
|
{
|
|
|
|
case k_tf_ssb_change_create:
|
|
|
|
break;
|
|
|
|
case k_tf_ssb_change_connect:
|
|
|
|
{
|
2021-09-06 14:23:22 -04:00
|
|
|
JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
2021-09-06 13:50:38 -04:00
|
|
|
JSValue args[] =
|
|
|
|
{
|
|
|
|
JS_NewString(context, "add"),
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
printf("calling function for ptr %p IsFunction=%d\n", user_data, JS_IsFunction(context, callback));
|
|
|
|
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
|
|
|
|
_check_call(context, response);
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeValue(context, args[0]);
|
|
|
|
JS_FreeValue(context, object);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case k_tf_ssb_change_remove:
|
|
|
|
{
|
2021-09-06 14:23:22 -04:00
|
|
|
JSValue object = JS_DupValue(context, tf_ssb_connection_get_object(connection));
|
2021-09-06 13:50:38 -04:00
|
|
|
JSValue args[] =
|
|
|
|
{
|
|
|
|
JS_NewString(context, "remove"),
|
|
|
|
object,
|
|
|
|
};
|
|
|
|
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
|
|
|
|
_check_call(context, response);
|
2021-09-06 14:23:22 -04:00
|
|
|
JS_FreeValue(context, args[0]);
|
|
|
|
JS_FreeValue(context, object);
|
2021-09-06 13:50:38 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _tf_ssb_register_connections_changed(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
|
|
|
{
|
|
|
|
printf("register connections changed\n");
|
|
|
|
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
|
|
|
if (!JS_IsFunction(context, argv[0]))
|
|
|
|
{
|
|
|
|
return JS_ThrowTypeError(context, "Expected argument 1 to be a function.");
|
|
|
|
}
|
|
|
|
void* ptr = JS_VALUE_GET_PTR(JS_DupValue(context, argv[0]));
|
|
|
|
printf("registering %p TAG=%d\n", ptr, JS_VALUE_GET_TAG(argv[0]));
|
|
|
|
tf_ssb_add_connections_changed_callback(ssb, _tf_ssb_rpc_on_connections_changed_callback, _tf_ssb_rpc_js_value_cleanup, ptr);
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_ssb_run_file(JSContext* context, const char* file_name)
|
|
|
|
{
|
|
|
|
FILE* file = fopen(file_name, "rb");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
printf("Unable to open %s: %s.", file_name, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* source = NULL;
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
long file_size = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
source = malloc(file_size + 1);
|
|
|
|
fread(source, 1, file_size, file);
|
|
|
|
source[file_size] = '\0';
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
JSValue result = JS_Eval(context, source, file_size, file_name, 0);
|
|
|
|
if (JS_IsError(context, result))
|
|
|
|
{
|
|
|
|
printf("Error running %s.\n", file_name);
|
|
|
|
const char* value = JS_ToCString(context, result);
|
|
|
|
printf("ERROR: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JSValue stack = JS_GetPropertyStr(context, result, "stack");
|
|
|
|
if (!JS_IsUndefined(stack)) {
|
|
|
|
const char* stack_str = JS_ToCString(context, stack);
|
|
|
|
printf("%s\n", stack_str);
|
|
|
|
JS_FreeCString(context, stack_str);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, stack);
|
|
|
|
}
|
|
|
|
else if (JS_IsException(result))
|
|
|
|
{
|
|
|
|
printf("Exception running %s.\n", file_name);
|
|
|
|
JSValue error = JS_GetException(context);
|
|
|
|
const char* value = JS_ToCString(context, error);
|
|
|
|
printf("Exception: %s\n", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
JS_FreeValue(context, error);
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, result);
|
|
|
|
free(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue _print(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
if (JS_IsNull(argv[i])) {
|
|
|
|
printf(" null");
|
|
|
|
} else {
|
|
|
|
const char* value = JS_ToCString(context, argv[i]);
|
|
|
|
printf(" %s", value);
|
|
|
|
JS_FreeCString(context, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _utf8Decode(JSContext* context, uint8_t* data, size_t length) {
|
|
|
|
return JS_NewStringLen(context, (const char*)data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue _utf8_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) {
|
|
|
|
JSValue result = JS_NULL;
|
|
|
|
size_t length;
|
|
|
|
if (JS_IsString(argv[0])) {
|
|
|
|
result = JS_DupValue(context, argv[0]);
|
|
|
|
} else {
|
|
|
|
uint8_t* array = tf_try_get_array_buffer(context, &length, argv[0]);
|
|
|
|
if (array) {
|
|
|
|
result = _utf8Decode(context, array, length);
|
|
|
|
} else {
|
|
|
|
size_t offset;
|
|
|
|
size_t element_size;
|
|
|
|
JSValue buffer = tf_try_get_typed_array_buffer(context, argv[0], &offset, &length, &element_size);
|
|
|
|
size_t size;
|
|
|
|
if (!JS_IsException(buffer)) {
|
|
|
|
array = tf_try_get_array_buffer(context, &size, buffer);
|
|
|
|
if (array) {
|
|
|
|
result = _utf8Decode(context, array, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_FreeValue(context, buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_ssb_init(JSContext* context, tf_ssb_t* ssb)
|
|
|
|
{
|
|
|
|
JS_NewClassID(&_tf_ssb_classId);
|
|
|
|
JSClassDef def = {
|
|
|
|
.class_name = "ssb",
|
|
|
|
};
|
|
|
|
if (JS_NewClass(JS_GetRuntime(context), _tf_ssb_classId, &def) != 0) {
|
|
|
|
fprintf(stderr, "Failed to register ssb.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
tf_ssb_set_broadcasts_changed_callback(ssb, _tf_ssb_broadcasts_changed, NULL);
|
2021-09-06 13:50:38 -04:00
|
|
|
tf_ssb_add_connections_changed_callback(ssb, _tf_ssb_connections_changed, NULL, NULL);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
JSValue global = JS_GetGlobalObject(context);
|
|
|
|
JSValue object = JS_NewObjectClass(context, _tf_ssb_classId);
|
|
|
|
JS_SetPropertyStr(context, global, "ssb", object);
|
|
|
|
JS_SetOpaque(object, ssb);
|
|
|
|
JS_SetPropertyStr(context, object, "whoami", JS_NewCFunction(context, _tf_ssb_whoami, "whoami", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "getMessage", JS_NewCFunction(context, _tf_ssb_getMessage, "getMessage", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunction(context, _tf_ssb_blobGet, "blobGet", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2));
|
2021-01-09 18:06:33 -05:00
|
|
|
JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "sqlStream", JS_NewCFunction(context, _tf_ssb_sqlStream, "sqlStream", 3));
|
|
|
|
JS_SetPropertyStr(context, object, "post", JS_NewCFunction(context, _tf_ssb_post, "post", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "appendMessage", JS_NewCFunction(context, _tf_ssb_appendMessage, "appendMessage", 1));
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));
|
|
|
|
JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _tf_ssb_connect, "connect", 1));
|
2021-09-06 13:50:38 -04:00
|
|
|
JS_SetPropertyStr(context, object, "registerRpc", JS_NewCFunction(context, _tf_ssb_register_rpc, "registerRpc", 2));
|
|
|
|
JS_SetPropertyStr(context, object, "registerBlobWantAdded", JS_NewCFunction(context, _tf_ssb_register_blob_want_added, "registerBlobWantAdded", 1));
|
|
|
|
JS_SetPropertyStr(context, object, "registerConnectionsChanged", JS_NewCFunction(context, _tf_ssb_register_connections_changed, "registerConnectionsChanged", 1));
|
|
|
|
|
|
|
|
JS_SetPropertyStr(context, global, "debug_print", JS_NewCFunction(context, _print, "debug_print", 2));
|
|
|
|
JS_SetPropertyStr(context, global, "debug_utf8Decode", JS_NewCFunction(context, _utf8_decode, "debug_utf8Decode", 1));
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
JS_FreeValue(context, global);
|
2021-09-06 13:50:38 -04:00
|
|
|
|
|
|
|
tf_ssb_run_file(context, "core/ssb.js");
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|