Skip to content

Commit a0b51fc

Browse files
authored
Merge pull request #19 from yougotwill/feat/ses-2243/constantsWrapper
Export libsession constants
2 parents 027e9e0 + 100a779 commit a0b51fc

File tree

13 files changed

+680
-15
lines changed

13 files changed

+680
-15
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
/build/
2-
/.vscode/
1+
# ide files
32
/.vscrof/
3+
/.vscode/
4+
/.idea/
5+
6+
/build/
47
/node_modules/
58
/*.tar.gz
69
/package-lock.json

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if(MSVC)
3030
endif()
3131

3232
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
33-
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "node_modules/node-addon-api" "../../node_modules/node-addon-api")
33+
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "node_modules/node-addon-api" "../../node_modules/node-addon-api" "node_modules/node-api-headers/include" "../../node_modules/node-api-headers/include")
3434

3535
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
3636
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} libsession::config libsession::crypto)

Readme.md renamed to README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ Note: The `electron` property in the `config` object will need to be updated in
3535

3636
### Making a Release and updating Session-desktop
3737

38-
First, make sure all your changes are commited and pushed to the `libsession-util-nodejs` project from your `[FOLDER_NOT_IN_SESSION_DESKTOP]` folder.
39-
Then, bump the version in the package.json of the nodejs wrapper.
38+
1. First, make sure all your changes are commited and pushed to the `libsession-util-nodejs` project from your `[FOLDER_NOT_IN_SESSION_DESKTOP]` folder.
4039

41-
- A **patch** version bump is required only if you have changed the implementation of an existing function or doing a hot fix for libsession version used by `session-desktop`.
40+
2. Then make sure the `libsession-util` is using the latest `dev` branch (unless there is a reason not to).
4241

43-
- A **minor** version bump is required if you have added a new function or changed the signature of an existing one.
42+
3. Then, bump the version in the package.json of the nodejs wrapper.
43+
44+
- A **patch** version bump is required only if you have changed the implementation of an existing function or doing a hot fix for libsession version used by `session-desktop`.
45+
46+
- A **minor** version bump is required if you have added a new function or changed the signature of an existing one.
4447

4548
Then, run these commands:
4649

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
"email": "[email protected]"
1010
},
1111
"scripts": {
12+
"clean": "rimraf .cache build",
1213
"install": "cmake-js compile --runtime=electron --runtime-version=25.8.4 -p16 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF"
1314
},
1415
"devDependencies": {
15-
"clang-format": "^1.8.0"
16+
"clang-format": "^1.8.0",
17+
"rimraf": "2.6.2"
1618
},
1719
"dependencies": {
1820
"cmake-js": "^7.2.1",
1921
"node-addon-api": "^6.1.0"
2022
},
2123
"typings": "index.d.ts"
22-
}
24+
}

shared.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,20 @@ declare module 'libsession_util_nodejs' {
6464
}
6565

6666
export type BaseWrapperActionsCalls = MakeWrapperActionCalls<BaseConfigWrapper>;
67+
68+
export type ConstantsType = {
69+
/** 100 bytes */
70+
CONTACT_MAX_NAME_LENGTH: number;
71+
/** 100 bytes - for legacy groups and communities */
72+
BASE_GROUP_MAX_NAME_LENGTH: number;
73+
/** 100 bytes */
74+
GROUP_INFO_MAX_NAME_LENGTH: number;
75+
/** 411 bytes
76+
*
77+
* BASE_URL_MAX_LENGTH + '/r/' + ROOM_MAX_LENGTH + qs_pubkey.size() + hex pubkey + null terminator
78+
*/
79+
COMMUNITY_FULL_URL_MAX_LENGTH: number;
80+
};
81+
82+
export const CONSTANTS: ConstantsType;
6783
}

src/addon.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <napi.h>
22

3+
#include "constants.hpp"
34
#include "contacts_config.hpp"
45
#include "convo_info_volatile_config.hpp"
56
#include "user_config.hpp"
@@ -8,6 +9,7 @@
89
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
910
using namespace session::nodeapi;
1011

12+
ConstantsWrapper::Init(env, exports);
1113
UserConfigWrapper::Init(env, exports);
1214
ContactsConfigWrapper::Init(env, exports);
1315
UserGroupsWrapper::Init(env, exports);

src/constants.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "constants.hpp"
2+
3+
#include "session/config/contacts.hpp"
4+
#include "session/config/groups/info.hpp"
5+
#include "session/config/user_groups.hpp"
6+
7+
namespace session::nodeapi {
8+
ConstantsWrapper::ConstantsWrapper(const Napi::CallbackInfo& info) :
9+
Napi::ObjectWrap<ConstantsWrapper>(info) {}
10+
11+
Napi::Object ConstantsWrapper::Init(Napi::Env env, Napi::Object exports) {
12+
const char* class_name = "CONSTANTS";
13+
14+
// construct javascript constants object
15+
Napi::Function cls = DefineClass(
16+
env,
17+
class_name,
18+
{ObjectWrap::StaticValue(
19+
"CONTACT_MAX_NAME_LENGTH",
20+
Napi::Number::New(env, session::config::contact_info::MAX_NAME_LENGTH),
21+
napi_enumerable),
22+
ObjectWrap::StaticValue(
23+
"BASE_GROUP_MAX_NAME_LENGTH",
24+
Napi::Number::New(env, session::config::base_group_info::NAME_MAX_LENGTH),
25+
napi_enumerable),
26+
ObjectWrap::StaticValue(
27+
"GROUP_INFO_MAX_NAME_LENGTH",
28+
Napi::Number::New(env, session::config::groups::Info::NAME_MAX_LENGTH),
29+
napi_enumerable),
30+
ObjectWrap::StaticValue(
31+
"COMMUNITY_FULL_URL_MAX_LENGTH",
32+
Napi::Number::New(env, session::config::community::FULL_URL_MAX_LENGTH),
33+
napi_enumerable)});
34+
35+
// export object as javascript module
36+
exports.Set(class_name, cls);
37+
return exports;
38+
}
39+
40+
} // namespace session::nodeapi

src/constants.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
5+
namespace session::nodeapi {
6+
class ConstantsWrapper : public Napi::ObjectWrap<ConstantsWrapper> {
7+
public:
8+
ConstantsWrapper(const Napi::CallbackInfo& info);
9+
10+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
11+
};
12+
} // namespace session::nodeapi

src/user_config.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ Napi::Value UserConfigWrapper::getUserInfo(const Napi::CallbackInfo& info) {
5555
});
5656
}
5757

58-
void UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
59-
wrapExceptions(info, [&] {
58+
Napi::Value UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
59+
return wrapResult(info, [&] {
6060
assertInfoLength(info, 3);
6161

6262
auto name = info[0];
@@ -70,7 +70,7 @@ void UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
7070
if (name.IsString())
7171
new_name = name.As<Napi::String>().Utf8Value();
7272

73-
config.set_name(new_name);
73+
config.set_name_truncated(new_name);
7474

7575
auto new_priority = toPriority(priority, config.get_nts_priority());
7676
config.set_nts_priority(new_priority);
@@ -79,6 +79,8 @@ void UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
7979
assertIsObject(profile_pic_obj);
8080

8181
config.set_profile_pic(profile_pic_from_object(profile_pic_obj));
82+
83+
return config.get_name();
8284
});
8385
}
8486

0 commit comments

Comments
 (0)