Skip to content

Commit

Permalink
Merge pull request #19 from yougotwill/feat/ses-2243/constantsWrapper
Browse files Browse the repository at this point in the history
Export libsession constants
  • Loading branch information
Bilb authored Jul 24, 2024
2 parents 027e9e0 + 100a779 commit a0b51fc
Show file tree
Hide file tree
Showing 13 changed files with 680 additions and 15 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/build/
/.vscode/
# ide files
/.vscrof/
/.vscode/
/.idea/

/build/
/node_modules/
/*.tar.gz
/package-lock.json
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if(MSVC)
endif()

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "node_modules/node-addon-api" "../../node_modules/node-addon-api")
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")

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} libsession::config libsession::crypto)
Expand Down
11 changes: 7 additions & 4 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ Note: The `electron` property in the `config` object will need to be updated in

### Making a Release and updating Session-desktop

First, make sure all your changes are commited and pushed to the `libsession-util-nodejs` project from your `[FOLDER_NOT_IN_SESSION_DESKTOP]` folder.
Then, bump the version in the package.json of the nodejs wrapper.
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.

- 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`.
2. Then make sure the `libsession-util` is using the latest `dev` branch (unless there is a reason not to).

- A **minor** version bump is required if you have added a new function or changed the signature of an existing one.
3. Then, bump the version in the package.json of the nodejs wrapper.

- 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`.

- A **minor** version bump is required if you have added a new function or changed the signature of an existing one.

Then, run these commands:

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
"email": "team@oxen.io"
},
"scripts": {
"clean": "rimraf .cache build",
"install": "cmake-js compile --runtime=electron --runtime-version=25.8.4 -p16 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF"
},
"devDependencies": {
"clang-format": "^1.8.0"
"clang-format": "^1.8.0",
"rimraf": "2.6.2"
},
"dependencies": {
"cmake-js": "^7.2.1",
"node-addon-api": "^6.1.0"
},
"typings": "index.d.ts"
}
}
16 changes: 16 additions & 0 deletions shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ declare module 'libsession_util_nodejs' {
}

export type BaseWrapperActionsCalls = MakeWrapperActionCalls<BaseConfigWrapper>;

export type ConstantsType = {
/** 100 bytes */
CONTACT_MAX_NAME_LENGTH: number;
/** 100 bytes - for legacy groups and communities */
BASE_GROUP_MAX_NAME_LENGTH: number;
/** 100 bytes */
GROUP_INFO_MAX_NAME_LENGTH: number;
/** 411 bytes
*
* BASE_URL_MAX_LENGTH + '/r/' + ROOM_MAX_LENGTH + qs_pubkey.size() + hex pubkey + null terminator
*/
COMMUNITY_FULL_URL_MAX_LENGTH: number;
};

export const CONSTANTS: ConstantsType;
}
2 changes: 2 additions & 0 deletions src/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <napi.h>

#include "constants.hpp"
#include "contacts_config.hpp"
#include "convo_info_volatile_config.hpp"
#include "user_config.hpp"
Expand All @@ -8,6 +9,7 @@
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
using namespace session::nodeapi;

ConstantsWrapper::Init(env, exports);
UserConfigWrapper::Init(env, exports);
ContactsConfigWrapper::Init(env, exports);
UserGroupsWrapper::Init(env, exports);
Expand Down
40 changes: 40 additions & 0 deletions src/constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "constants.hpp"

#include "session/config/contacts.hpp"
#include "session/config/groups/info.hpp"
#include "session/config/user_groups.hpp"

namespace session::nodeapi {
ConstantsWrapper::ConstantsWrapper(const Napi::CallbackInfo& info) :
Napi::ObjectWrap<ConstantsWrapper>(info) {}

Napi::Object ConstantsWrapper::Init(Napi::Env env, Napi::Object exports) {
const char* class_name = "CONSTANTS";

// construct javascript constants object
Napi::Function cls = DefineClass(
env,
class_name,
{ObjectWrap::StaticValue(
"CONTACT_MAX_NAME_LENGTH",
Napi::Number::New(env, session::config::contact_info::MAX_NAME_LENGTH),
napi_enumerable),
ObjectWrap::StaticValue(
"BASE_GROUP_MAX_NAME_LENGTH",
Napi::Number::New(env, session::config::base_group_info::NAME_MAX_LENGTH),
napi_enumerable),
ObjectWrap::StaticValue(
"GROUP_INFO_MAX_NAME_LENGTH",
Napi::Number::New(env, session::config::groups::Info::NAME_MAX_LENGTH),
napi_enumerable),
ObjectWrap::StaticValue(
"COMMUNITY_FULL_URL_MAX_LENGTH",
Napi::Number::New(env, session::config::community::FULL_URL_MAX_LENGTH),
napi_enumerable)});

// export object as javascript module
exports.Set(class_name, cls);
return exports;
}

} // namespace session::nodeapi
12 changes: 12 additions & 0 deletions src/constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <napi.h>

namespace session::nodeapi {
class ConstantsWrapper : public Napi::ObjectWrap<ConstantsWrapper> {
public:
ConstantsWrapper(const Napi::CallbackInfo& info);

static Napi::Object Init(Napi::Env env, Napi::Object exports);
};
} // namespace session::nodeapi
8 changes: 5 additions & 3 deletions src/user_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Napi::Value UserConfigWrapper::getUserInfo(const Napi::CallbackInfo& info) {
});
}

void UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
wrapExceptions(info, [&] {
Napi::Value UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
return wrapResult(info, [&] {
assertInfoLength(info, 3);

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

config.set_name(new_name);
config.set_name_truncated(new_name);

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

config.set_profile_pic(profile_pic_from_object(profile_pic_obj));

return config.get_name();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/user_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserConfigWrapper : public ConfigBaseImpl, public Napi::ObjectWrap<UserCon
config::UserProfile& config{get_config<config::UserProfile>()};

Napi::Value getUserInfo(const Napi::CallbackInfo& info);
void setUserInfo(const Napi::CallbackInfo& info);
Napi::Value setUserInfo(const Napi::CallbackInfo& info);

Napi::Value getEnableBlindedMsgRequest(const Napi::CallbackInfo& info);
void setEnableBlindedMsgRequest(const Napi::CallbackInfo& info);
Expand Down
3 changes: 2 additions & 1 deletion user/userconfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ declare module 'libsession_util_nodejs' {
url: string;
key: Uint8Array;
};
/** if name > CONTACT_MAX_NAME_LENGTH it will be truncated */
setUserInfo: (
name: string,
priority: number,
profilePic: { url: string; key: Uint8Array } | null
) => void;
) => string;
setEnableBlindedMsgRequest: (msgRequest: boolean) => void;
getEnableBlindedMsgRequest: () => boolean | undefined;
setNoteToSelfExpiry: (expirySeconds: number) => void;
Expand Down
Loading

0 comments on commit a0b51fc

Please sign in to comment.