From 9d35b4bdfb3c47bd552dfcf2309c9108a80c7730 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 10 Jun 2024 11:45:20 -0400 Subject: [PATCH] Resuming work to move all DB access off the main thread. --- run.log | 1062 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 1 + src/ssb.db.c | 38 ++ src/ssb.db.h | 16 + src/ssb.js.c | 34 +- src/ssb.rpc.c | 63 ++- 6 files changed, 1191 insertions(+), 23 deletions(-) create mode 100644 run.log diff --git a/run.log b/run.log new file mode 100644 index 00000000..a57fce61 --- /dev/null +++ b/run.log @@ -0,0 +1,1062 @@ +File not foundHello, world! +Hello, world! +Hello, world! +Hello, world! +Hello, world! +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +== running test bip39 == +1: body hair useful camp warm into cause riot two bamboo kick educate dinosaur advice seed type crisp where guilt avocado output rely lunch goddess +[pass] bip39 +== running test http == +Starting http. +curl returned 0 +curl returned 0 +size = 11 body=hello world +curl returned 0 +curl returned 0 +Done running. +[pass] http +== running test ssb == +Testing SSB. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +ID @3bV0UxKK/AFwbDSw3Cfx3eXgjL6gNXy5HSwty0sLFFU=.ed25519 and @0jpl9/v9YIG3yjHF+XFM5M/iYCAMfB2TLmK7x0nZE0A=.ed25519 +blob stored &hgjLeSZeURdqZmq/k+zyqiIUr5RMU2ztJbzUcSHuUHQ=.sha256 12 => 1 +Waiting for connection. +callback1 change=0 connection=0x7c20e31c4808 +conns = 0 0 0 +callback0 change=0 connection=0x7c20e27a1808 +conns = 0 0 0 +callback0 change=1 connection=0x7c20e27a1808 +conns = 1 0 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 0 0 +callback1 change=1 connection=0x7c20e31c4808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +Stopped broadcasts. +Waiting for messages. +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +Waiting for blob. +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 1 0 +s1 = 12 sl = 12 +Waiting for message to self. +Waiting for message from other. +done +Connection cli0 0x7c20e31c4808 is closing: crypto_secretbox_easy close message. +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 0 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 0 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 0 0 +callback1 change=3 connection=0x7c20e31c4808 +conns = 1 0 0 +callback1 change=2 connection=0x7c20e31c4808 +conns = 1 0 0 +final run +Connection srv1 0x7c20e27a1808 is closing: end of file. +callback0 change=3 connection=0x7c20e27a1808 +conns = 0 0 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 0 0 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 0 0 0 +callback0 change=3 connection=0x7c20e27a1808 +conns = 0 0 0 +callback0 change=2 connection=0x7c20e27a1808 +conns = 0 0 0 +done +destroy 0 +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +destroy 1 +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +close +[pass] ssb +== running test ssb_id == +Testing id conversion. +[pass] ssb_id +== running test ssb_following == +Testing following. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +[pass] ssb_following +== running test nop == +out/debug/tildefriends run --db-path=:meexepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> hi +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +ERROR: Error: oops + at (out/test.js:1) + +ERROR: Error: oops + at (out/test.js:1) + +Reported an error. +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +Task[0x626000000108:]> Poking the sandbox. This should fail. +Poking sandbox. +Sandbox poked. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> child exited 127 0 +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +Task[0x626000000108:]> I am the child process. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> child exited 0 0 +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> Caught: +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> Caught: oops +Task[0x626000000108:out/test.js]> stack: at (child.js:4) + at Promise (native) + at add (child.js:5) + +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +will delete more blobs in 30000 ms +Running script out/test.js +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1ba1e src/ssb.c:2270 tf_ssb_acquire_db_reader +0x55fd0fdd4851 src/database.js.c:102 _database_get +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1c871 src/ssb.c:2308 tf_ssb_acquire_db_writer +0x55fd0fdd511b src/database.js.c:127 _database_set +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1ba1e src/ssb.c:2270 tf_ssb_acquire_db_reader +0x55fd0fdd4851 src/database.js.c:102 _database_get +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1c871 src/ssb.c:2308 tf_ssb_acquire_db_writer +0x55fd0fdd511b src/database.js.c:127 _database_set +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1c871 src/ssb.c:2308 tf_ssb_acquire_db_writer +0x55fd0fdd511b src/database.js.c:127 _database_set +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +Acquiring DB from the main thread: +0x7e7fd045401e ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:4277 __interceptor_backtrace +0x55fd0fea1bd7 src/util.js.c:477 tf_util_backtrace +0x55fd0fea1c7f src/util.js.c:434 tf_util_backtrace_string +0x55fd0fe12aec src/ssb.c:2260 _tf_ssb_assert_not_main_thread +0x55fd0fe1ba1e src/ssb.c:2270 tf_ssb_acquire_db_reader +0x55fd0fdd4466 src/database.js.c:230 _database_get_all +0x55fd100cc169 deps/quickjs/quickjs.c:15913 js_call_c_function +0x55fd0ffde160 deps/quickjs/quickjs.c:16108 JS_CallInternal +0x55fd0ffe8d2c deps/quickjs/quickjs.c:16515 JS_CallInternal +0x55fd1012adc4 deps/quickjs/quickjs.c:18823 async_func_resume +0x55fd101402e6 deps/quickjs/quickjs.c:19095 js_async_function_resume +0x55fd10153af3 deps/quickjs/quickjs.c:19191 js_async_function_call +0x55fd101540bd deps/quickjs/quickjs.c:28737 js_execute_sync_module +0x55fd10155c32 deps/quickjs/quickjs.c:28849 js_inner_module_evaluation +0x55fd10156d6a deps/quickjs/quickjs.c:28896 js_evaluate_module +0x55fd101579ee deps/quickjs/quickjs.c:34221 JS_EvalFunctionInternal +0x55fd101596bf deps/quickjs/quickjs.c:34346 __JS_EvalInternal +0x55fd1001fd7c deps/quickjs/quickjs.c:34364 JS_EvalInternal +0x55fd1001fe03 deps/quickjs/quickjs.c:34398 JS_EvalThis +0x55fd1001fea5 deps/quickjs/quickjs.c:34405 JS_Eval +0x55fd0fe81749 src/task.c:423 tf_task_execute +0x55fd0fdf99d0 src/main.c:385 _tf_run_task +0x55fd0fdfa58e src/main.c:575 _tf_command_run +0x55fd0fdfae31 src/main.c:781 main +0x7e7fcf229d90 ../sysdeps/nptl/libc_start_call_main.h:58 __libc_start_call_main +0x7e7fcf229e40 ../csu/libc-start.c:128 call_init +0x7e7fcf229e40 ../csu/libc-start.c:379 __libc_start_main_impl +0x55fd0fdd1ed5 (null):0 (null) + +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> hi +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/bad.js +ERROR: ReferenceError: Could not load 'out/missing.js'. + +Reported an error. +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> Hi +Task[0x626000000108:out/test.js]> 3 +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> child started +Task[0x626000000108:out/test.js]> input 0,1,2,3,4,5,6,7,8,9 10 10 +Task[0x626000000108:out/test.js]> output 0,1,2,3,4,5,6,7,8,9 10 10 +Task[0x626000000108:out/test.js]> 0 +Task[0x626000000108:out/test.js]> 1 +Task[0x626000000108:out/test.js]> 2 +Task[0x626000000108:out/test.js]> 3 +Task[0x626000000108:out/test.js]> 4 +Task[0x626000000108:out/test.js]> 5 +Task[0x626000000108:out/test.js]> 6 +Task[0x626000000108:out/test.js]> 7 +Task[0x626000000108:out/test.js]> 8 +Task[0x626000000108:out/test.js]> 9 +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> get exports +Task[0x626000000108:out/test.js]> calling export +Task[0x626000000108:out/test.js]> 1.2 +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> connecting +Task[0x626000000108:out/test.js]> before connect false +Task[0x626000000108:out/test.js]> noDelay false +Task[0x626000000108:out/test.js]> connecting +Task[0x626000000108:out/test.js]> before connect false +Task[0x626000000108:out/test.js]> noDelay false +Task[0x626000000108:out/test.js]> connecting s3 +Task[0x626000000108:out/test.js]> before connect false +Task[0x626000000108:out/test.js]> noDelay false +Task[0x626000000108:out/test.js]> caught +Task[0x626000000108:out/test.js]> InternalError: uv_tcp_connect: connection refused +Task[0x626000000108:out/test.js]> connected www.unprompted.com 80 true +Task[0x626000000108:out/test.js]> 66.175.209.35 +Task[0x626000000108:out/test.js]> connected www.unprompted.com 443 +Task[0x626000000108:out/test.js]> closed 1 +Task[0x626000000108:out/test.js]> read 543 +Task[0x626000000108:out/test.js]> read null +Task[0x626000000108:out/test.js]> ready +Task[0x626000000108:out/test.js]> 66.175.209.35 +Task[0x626000000108:out/test.js]> read 492 +Task[0x626000000108:out/test.js]> read null +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> expected error InternalError: Failed to open out/missing.txt for read: no such file or directory +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +exepath is /home/cory/src/tildefriends/out/debug/tildefriends +setting zip path to (null) +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +prepare failed: no such table: properties +prepare failed: no such table: properties +will delete more blobs in 30000 ms +Running script out/test.js +Task[0x626000000108:out/test.js]> aGVsbG8= +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +Goodbye. +_tf_run_task is done. Goodbye. +mory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] nop +== running test exception == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +result = 65280 +[pass] exception +== running test sandbox == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] sandbox +== running test child == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] child +== running test promise == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] promise +== running test promise_remote_throw == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] promise_remote_throw +== running test promise_remote_reject == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] promise_remote_reject +== running test database == +out/debug/tildefriends run --db-path=out/test_db0.sqlite -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] database +== running test this == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] this +== running test await == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] await +== running test import == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +out/debug/tildefriends run --db-path=:memory: -s out/bad.js --ssb-port=0 --http-port=0 --https-port=0 +returned 255 +[pass] import +== running test exit == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] exit +== running test icu == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] icu +== running test uint8array == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] uint8array +== running test float == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +[pass] float +== running test socket == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] socket +== running test file == +out/debug/tildefriends run --db-path=out/test.db -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] file +== running test b64 == +out/debug/tildefriends run --db-path=:memory: -s out/test.js --ssb-port=0 --http-port=0 --https-port=0 +returned 0 +[pass] b64 +== running test rooms == +Testing Rooms. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +ID @VXbugSv722f0Iy6uKdYYs1HIRYeNR+/eS76+7s9jL1k=.ed25519, @WkLs3nXZD7OGE1m3JCfTjIV44YIZLpDoVcks3m5Rqu0=.ed25519, @OZmTKyFxx1nCYZY3HPxfZAVn5iKYLYCqkKeP7CA6VSo=.ed25519 +Waiting for connection. +callback1 change=0 connection=0x7c20e16da808 +conns = 0 0 0 +callback2 change=0 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback0 change=0 connection=0x7c20e0294808 +conns = 0 0 0 +callback0 change=0 connection=0x7c20df871808 +conns = 0 0 0 +callback0 change=1 connection=0x7c20e0294808 +conns = 1 0 0 +callback0 change=3 connection=0x7c20e0294808 +conns = 1 0 0 +callback1 change=1 connection=0x7c20e16da808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 1 0 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 1 0 +callback0 change=1 connection=0x7c20df871808 +conns = 2 1 0 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 0 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 0 +callback2 change=1 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +Stopped broadcasts. +Waiting for broadcasts. +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 1 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +BROADCASTS 0 0 0 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +BROADCASTS 0 0 0 +BROADCASTS 0 1 0 +BROADCASTS 0 1 1 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 1 +callback1 change=0 connection=0x7c20deae6808 +conns = 2 1 1 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 1 +tun0 = 0x7c20deae6808 +Done. +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 2 1 1 +callback0 change=3 connection=0x7c20df871808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=0 connection=0x7c20de04a808 +conns = 2 1 1 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 2 1 1 +callback2 change=1 connection=0x7c20de04a808 +conns = 2 1 2 +callback2 change=3 connection=0x7c20de04a808 +conns = 2 1 2 +callback1 change=1 connection=0x7c20deae6808 +conns = 2 2 2 +callback1 change=3 connection=0x7c20deae6808 +conns = 2 2 2 +callback1 change=3 connection=0x7c20deae6808 +conns = 2 2 2 +callback1 change=3 connection=0x7c20deae6808 +conns = 2 2 2 +Done. +Connection tun0 0x7c20deae6808 is closing: tf_ssb_connection_close. +callback1 change=3 connection=0x7c20deae6808 +conns = 2 1 2 +callback1 change=3 connection=0x7c20deae6808 +conns = 2 1 2 +callback1 change=3 connection=0x7c20deae6808 +conns = 2 1 2 +callback1 change=2 connection=0x7c20deae6808 +conns = 2 1 2 +callback1 change=3 connection=0x7c20e16da808 +conns = 2 1 2 +Connection srv5 0x7c20df871808 is closing: crypto_secretbox_easy close message. +callback0 change=3 connection=0x7c20df871808 +conns = 1 1 2 +callback0 change=3 connection=0x7c20df871808 +conns = 1 1 2 +callback0 change=3 connection=0x7c20df871808 +conns = 1 1 2 +callback0 change=3 connection=0x7c20df871808 +conns = 1 1 2 +callback0 change=3 connection=0x7c20df871808 +conns = 1 1 2 +callback0 change=2 connection=0x7c20df871808 +conns = 1 1 2 +Connection cli2 0x7c20e16da808 is closing: crypto_secretbox_easy close message. +callback1 change=3 connection=0x7c20e16da808 +conns = 1 0 2 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 0 2 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 0 2 +callback1 change=3 connection=0x7c20e16da808 +conns = 1 0 2 +callback1 change=2 connection=0x7c20e16da808 +conns = 1 0 2 +Connection tun1 0x7c20de04a808 is closing: crypto_secretbox_easy close message. +callback2 change=3 connection=0x7c20de04a808 +conns = 1 0 1 +callback2 change=2 connection=0x7c20de04a808 +conns = 1 0 1 +Connection srv4 0x7c20e0294808 is closing: tf_ssb_close_all. +callback0 change=3 connection=0x7c20e0294808 +conns = 0 0 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 0 0 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 0 0 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 0 0 1 +callback0 change=3 connection=0x7c20e0294808 +conns = 0 0 1 +callback0 change=2 connection=0x7c20e0294808 +conns = 0 0 1 +Connection cli3 0x7c20e0cb7808 is closing: tf_ssb_close_all. +callback2 change=3 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback2 change=3 connection=0x7c20e0cb7808 +conns = 0 0 0 +callback2 change=2 connection=0x7c20e0cb7808 +conns = 0 0 0 +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +[pass] rooms +== running test bench == +Testing following. +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +insert = 11.531131 seconds +Enabling auto-vacuum and performing full vacuum. +All clean. +Populating full-text search... +Done. +Populating messages_refs... +Done. +Generating a new keypair. +Waiting for messages. +Done. +replicate = 5.515292 seconds +Connection cli6 0x7c20e30b7808 is closing: crypto_secretbox_easy close message. +Stopped broadcasts. +Connection srv7 0x7c20e16da808 is closing: end of file. +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +tf_ssb_destroy +Waiting for closes. +Waiting for rpc. +Closing connections. +Closed. +uv loop closed. +closing ssb context +[pass] bench +== running test auto == +Process exit 0 signal=0. +Process exit 0 signal=0. +[pass] auto +Tests completed. diff --git a/src/main.c b/src/main.c index c3812895..09df8e6c 100644 --- a/src/main.c +++ b/src/main.c @@ -381,6 +381,7 @@ static int _tf_run_task(const tf_run_args_t* args, int index) tf_ssb_import(tf_task_get_ssb(task), "core", "apps"); } } + tf_ssb_set_main_thread(tf_task_get_ssb(task), true); if (tf_task_execute(task, args->script)) { tf_task_run(task); diff --git a/src/ssb.db.c b/src/ssb.db.c index e82fa5ac..a7e6f5a8 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -635,6 +635,44 @@ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_ return result; } +typedef struct _blob_get_async_t +{ + tf_ssb_t* ssb; + char id[k_blob_id_len]; + tf_ssb_db_blob_get_callback_t* callback; + void* user_data; + + bool out_found; + uint8_t* out_data; + size_t out_size; +} blob_get_async_t; + +static void _tf_ssb_db_blob_get_async_work(tf_ssb_t* ssb, void* user_data) +{ + blob_get_async_t* async = user_data; + async->out_found = tf_ssb_db_blob_get(ssb, async->id, &async->out_data, &async->out_size); +} + +static void _tf_ssb_db_blob_get_async_after_work(tf_ssb_t* ssb, int status, void* user_data) +{ + blob_get_async_t* async = user_data; + async->callback(async->out_found, async->out_data, async->out_size, async->user_data); + tf_free(async->out_data); + tf_free(async); +} + +void tf_ssb_db_blob_get_async(tf_ssb_t* ssb, const char* id, tf_ssb_db_blob_get_callback_t* callback, void* user_data) +{ + blob_get_async_t* async = tf_malloc(sizeof(blob_get_async_t)); + *async = (blob_get_async_t) { + .ssb = ssb, + .callback = callback, + .user_data = user_data, + }; + snprintf(async->id, sizeof(async->id), "%s", id); + tf_ssb_run_work(ssb, _tf_ssb_db_blob_get_async_work, _tf_ssb_db_blob_get_async_after_work, async); +} + typedef struct _blob_store_work_t { const uint8_t* blob; diff --git a/src/ssb.db.h b/src/ssb.db.h index 58a3b400..d80a5445 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -55,6 +55,22 @@ bool tf_ssb_db_blob_has(tf_ssb_t* ssb, const char* id); */ bool tf_ssb_db_blob_get(tf_ssb_t* ssb, const char* id, uint8_t** out_blob, size_t* out_size); +/** +** A function called when a blob is retrieved from the database. +** @param id The blob identifier. +** @param user_data The user data. +*/ +typedef void(tf_ssb_db_blob_get_callback_t)(bool found, const uint8_t* data, size_t size, void* user_data); + +/** +** Retrieve a blob from the database asynchronously. +** @param ssb The SSB instance. +** @param id The blob identifier. +** @param callback Callback called with the result. +** @param user_data The user data. +*/ +void tf_ssb_db_blob_get_async(tf_ssb_t* ssb, const char* id, tf_ssb_db_blob_get_callback_t* callback, void* user_data); + /** ** A function called when a message is stored in the database. ** @param id The message identifier. diff --git a/src/ssb.js.c b/src/ssb.js.c index 22953f2c..eed53aec 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -581,6 +581,29 @@ static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueCons return result; } +typedef struct _blob_get_t +{ + JSContext* context; + JSValue promise[2]; +} blob_get_t; + +static void _tf_ssb_blobGet_callback(bool found, const uint8_t* data, size_t size, void* user_data) +{ + blob_get_t* get = user_data; + JSValue result = JS_UNDEFINED; + if (found) + { + result = JS_NewArrayBufferCopy(get->context, data, size); + } + JSValue error = JS_Call(get->context, get->promise[0], JS_UNDEFINED, 1, &result); + JS_FreeValue(get->context, result); + JS_FreeValue(get->context, get->promise[0]); + JS_FreeValue(get->context, get->promise[1]); + tf_util_report_error(get->context, error); + JS_FreeValue(get->context, error); + tf_free(get); +} + static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { JSValue result = JS_NULL; @@ -588,13 +611,10 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar if (ssb) { const char* id = JS_ToCString(context, argv[0]); - uint8_t* blob = NULL; - size_t size = 0; - if (tf_ssb_db_blob_get(ssb, id, &blob, &size)) - { - result = JS_NewArrayBufferCopy(context, blob, size); - tf_free(blob); - } + blob_get_t* get = tf_malloc(sizeof(blob_get_t)); + *get = (blob_get_t) { .context = context }; + result = JS_NewPromiseCapability(context, get->promise); + tf_ssb_db_blob_get_async(ssb, id, _tf_ssb_blobGet_callback, get); JS_FreeCString(context, id); } return result; diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index 9cade1fe..bbf4203e 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -480,6 +480,45 @@ static void _tf_ssb_rpc_connection_blobs_get(tf_ssb_connection_t* connection, co JS_FreeValue(context, message); } +typedef struct _blob_create_wants_work_t +{ + tf_ssb_connection_t* connection; + char blob_id[k_blob_id_len]; + bool out_result; + int64_t size; + size_t out_size; +} blob_create_wants_work_t; + +static void _tf_ssb_rpc_connection_blobs_create_wants_work(tf_ssb_connection_t* connection, void* user_data) +{ + blob_create_wants_work_t* work = user_data; + tf_ssb_t* ssb = tf_ssb_connection_get_ssb(work->connection); + work->out_result = tf_ssb_db_blob_get(ssb, work->blob_id, NULL, &work->out_size); +} + +static void _tf_ssb_rpc_connection_blobs_create_wants_after_work(tf_ssb_connection_t* connection, int result, void* user_data) +{ + blob_create_wants_work_t* work = user_data; + tf_ssb_t* ssb = tf_ssb_connection_get_ssb(work->connection); + tf_ssb_blob_wants_t* blob_wants = tf_ssb_connection_get_blob_wants_state(connection); + JSContext* context = tf_ssb_get_context(ssb); + if (work->out_result) + { + JSValue message = JS_NewObject(context); + JS_SetPropertyStr(context, message, work->blob_id, JS_NewInt64(context, work->out_size)); + tf_ssb_connection_rpc_send_json(work->connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL); + JS_FreeValue(context, message); + } + else if (work->size == -1LL) + { + JSValue message = JS_NewObject(context); + JS_SetPropertyStr(context, message, work->blob_id, JS_NewInt64(context, -2)); + tf_ssb_connection_rpc_send_json(work->connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL); + JS_FreeValue(context, message); + } + tf_free(work); +} + static void _tf_ssb_rpc_connection_blobs_createWants_callback( tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data) { @@ -489,7 +528,6 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback( return; } - tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); JSContext* context = tf_ssb_connection_get_context(connection); JSValue name = JS_GetPropertyStr(context, args, "name"); @@ -524,21 +562,14 @@ static void _tf_ssb_rpc_connection_blobs_createWants_callback( } if (size < 0) { - size_t blob_size = 0; - if (tf_ssb_db_blob_get(ssb, blob_id, NULL, &blob_size)) - { - JSValue message = JS_NewObject(context); - JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, blob_size)); - tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL); - JS_FreeValue(context, message); - } - else if (size == -1LL) - { - JSValue message = JS_NewObject(context); - JS_SetPropertyStr(context, message, blob_id, JS_NewInt64(context, -2)); - tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_stream, -blob_wants->request_number, NULL, message, NULL, NULL, NULL); - JS_FreeValue(context, message); - } + blob_create_wants_work_t* work = tf_malloc(sizeof(blob_create_wants_work_t)); + *work = (blob_create_wants_work_t) { + .connection = connection, + .size = size, + .out_size = -1ULL, + }; + snprintf(work->blob_id, sizeof(work->blob_id), "%s", blob_id); + tf_ssb_connection_run_work(connection, _tf_ssb_rpc_connection_blobs_create_wants_work, _tf_ssb_rpc_connection_blobs_create_wants_after_work, work); } else {