Skip to content

Commit d1f2668

Browse files
committed
fix: constants now works correctly with correct typings!
updated gitignore file
1 parent 360f32c commit d1f2668

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
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

shared.d.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ declare module 'libsession_util_nodejs' {
6464
}
6565

6666
export type BaseWrapperActionsCalls = MakeWrapperActionCalls<BaseConfigWrapper>;
67-
}
68-
69-
/**
70-
* Constants wrapper logic
71-
*/
7267

73-
export type ConstantsWrapper = {
74-
"CONSTANTS": {
75-
MAX_NAME_LENGTH: number
68+
export type ConstantsType = {
69+
/** Maximum byte length for user, contact and group names */
70+
MAX_NAME_LENGTH: number;
7671
}
72+
73+
export const CONSTANTS: ConstantsType;
7774
}

src/constants.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
#include "constants.hpp"
22

3-
namespace session::nodeapi {
4-
Napi::Number ConstantsWrapper::MAX_NAME_LENGTH;
3+
#include "session/config/contacts.hpp"
54

5+
namespace session::nodeapi {
66
ConstantsWrapper::ConstantsWrapper(const Napi::CallbackInfo& info) :
77
Napi::ObjectWrap<ConstantsWrapper>(info) {}
88

99
Napi::Object ConstantsWrapper::Init(Napi::Env env, Napi::Object exports) {
10-
MAX_NAME_LENGTH = Napi::Number::New(env, session::config::contact_info::MAX_NAME_LENGTH);
10+
// fetch cpp constants
11+
Napi::Number MAX_NAME_LENGTH =
12+
Napi::Number::New(env, session::config::contact_info::MAX_NAME_LENGTH);
1113

12-
Napi::Object constants = Napi::Object::New(env);
13-
constants.Set("MAX_NAME_LENGTH", MAX_NAME_LENGTH);
14+
const char* class_name = "CONSTANTS";
1415

15-
exports.Set("CONSTANTS", constants);
16+
// construct javascript constants object
17+
Napi::Function cls = DefineClass(
18+
env,
19+
class_name,
20+
{ObjectWrap::StaticValue("MAX_NAME_LENGTH", MAX_NAME_LENGTH, napi_enumerable)});
21+
22+
// export object as javascript module
23+
exports.Set(class_name, cls);
1624
return exports;
1725
}
1826

1927
} // namespace session::nodeapi
28+

src/constants.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
#include <napi.h>
44

5-
#include "session/config/contacts.hpp"
6-
75
namespace session::nodeapi {
86
class ConstantsWrapper : public Napi::ObjectWrap<ConstantsWrapper> {
97
public:
10-
static Napi::Object Init(Napi::Env env, Napi::Object exports);
118
ConstantsWrapper(const Napi::CallbackInfo& info);
129

13-
private:
14-
static Napi::Number MAX_NAME_LENGTH;
10+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
1511
};
1612
} // namespace session::nodeapi
13+

0 commit comments

Comments
 (0)