1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "Const_Properties"
16 #include "js_const_properties.h"
17 #include "js_util.h"
18 #include "js_single_kv_store.h"
19 #include "log_print.h"
20 #include "types.h"
21
22 using namespace OHOS::DistributedKv;
23 namespace OHOS::DistributedKVStore {
SetNamedProperty(napi_env env,napi_value & obj,const std::string & name,int32_t value)24 static napi_status SetNamedProperty(napi_env env, napi_value& obj, const std::string& name, int32_t value)
25 {
26 napi_value property = nullptr;
27 napi_status status = napi_create_int32(env, value, &property);
28 ASSERT(status == napi_ok, "int32_t to napi_value failed!", status);
29 status = napi_set_named_property(env, obj, name.c_str(), property);
30 ASSERT(status == napi_ok, "napi_set_named_property failed!", status);
31 return status;
32 }
33
ExportConstants(napi_env env)34 static napi_value ExportConstants(napi_env env)
35 {
36 constexpr int32_t MAX_KEY_LENGTH = 1024;
37 constexpr int32_t MAX_VALUE_LENGTH = 4194303;
38 constexpr int32_t MAX_KEY_LENGTH_DEVICE = 896;
39 constexpr int32_t MAX_STORE_ID_LENGTH = 128;
40 constexpr int32_t MAX_QUERY_LENGTH = 512000;
41 constexpr int32_t MAX_BATCH_SIZE = 128;
42
43 napi_value constants = nullptr;
44 napi_create_object(env, &constants);
45 SetNamedProperty(env, constants, "MAX_KEY_LENGTH", MAX_KEY_LENGTH);
46 SetNamedProperty(env, constants, "MAX_VALUE_LENGTH", MAX_VALUE_LENGTH);
47 SetNamedProperty(env, constants, "MAX_KEY_LENGTH_DEVICE", MAX_KEY_LENGTH_DEVICE);
48 SetNamedProperty(env, constants, "MAX_STORE_ID_LENGTH", MAX_STORE_ID_LENGTH);
49 SetNamedProperty(env, constants, "MAX_QUERY_LENGTH", MAX_QUERY_LENGTH);
50 SetNamedProperty(env, constants, "MAX_BATCH_SIZE", MAX_BATCH_SIZE);
51 napi_object_freeze(env, constants);
52 return constants;
53 }
54
ExportValueType(napi_env env)55 static napi_value ExportValueType(napi_env env)
56 {
57 napi_value valueType = nullptr;
58 napi_create_object(env, &valueType);
59 SetNamedProperty(env, valueType, "STRING", (int32_t)JSUtil::STRING);
60 SetNamedProperty(env, valueType, "INTEGER", (int32_t)JSUtil::INTEGER);
61 SetNamedProperty(env, valueType, "FLOAT", (int32_t)JSUtil::FLOAT);
62 SetNamedProperty(env, valueType, "BYTE_ARRAY", (int32_t)JSUtil::BYTE_ARRAY);
63 SetNamedProperty(env, valueType, "BOOLEAN", (int32_t)JSUtil::BOOLEAN);
64 SetNamedProperty(env, valueType, "DOUBLE", (int32_t)JSUtil::DOUBLE);
65 napi_object_freeze(env, valueType);
66 return valueType;
67 }
68
ExportSyncMode(napi_env env)69 static napi_value ExportSyncMode(napi_env env)
70 {
71 napi_value syncMode = nullptr;
72 napi_create_object(env, &syncMode);
73 SetNamedProperty(env, syncMode, "PULL_ONLY", (int32_t)SyncMode::PULL);
74 SetNamedProperty(env, syncMode, "PUSH_ONLY", (int32_t)SyncMode::PUSH);
75 SetNamedProperty(env, syncMode, "PUSH_PULL", (int32_t)SyncMode::PUSH_PULL);
76 napi_object_freeze(env, syncMode);
77 return syncMode;
78 }
79
ExportSubscribeType(napi_env env)80 static napi_value ExportSubscribeType(napi_env env)
81 {
82 napi_value subscribeType = nullptr;
83 napi_create_object(env, &subscribeType);
84
85 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_LOCAL", (int32_t)SUBSCRIBE_LOCAL);
86 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_REMOTE", (int32_t)SUBSCRIBE_REMOTE);
87 SetNamedProperty(env, subscribeType, "SUBSCRIBE_TYPE_ALL", (int32_t)SUBSCRIBE_LOCAL_REMOTE);
88 napi_object_freeze(env, subscribeType);
89 return subscribeType;
90 }
91
ExportKVStoreType(napi_env env)92 static napi_value ExportKVStoreType(napi_env env)
93 {
94 napi_value kvStoreType = nullptr;
95 napi_create_object(env, &kvStoreType);
96 SetNamedProperty(env, kvStoreType, "DEVICE_COLLABORATION", (int32_t)KvStoreType::DEVICE_COLLABORATION);
97 SetNamedProperty(env, kvStoreType, "SINGLE_VERSION", (int32_t)KvStoreType::SINGLE_VERSION);
98 napi_object_freeze(env, kvStoreType);
99 return kvStoreType;
100 }
101
ExportSecurityLevel(napi_env env)102 static napi_value ExportSecurityLevel(napi_env env)
103 {
104 napi_value securityLevel = nullptr;
105
106 napi_create_object(env, &securityLevel);
107 SetNamedProperty(env, securityLevel, "S1", (int32_t)SecurityLevel::S1);
108 SetNamedProperty(env, securityLevel, "S2", (int32_t)SecurityLevel::S2);
109 SetNamedProperty(env, securityLevel, "S3", (int32_t)SecurityLevel::S3);
110 SetNamedProperty(env, securityLevel, "S4", (int32_t)SecurityLevel::S4);
111 napi_object_freeze(env, securityLevel);
112 return securityLevel;
113 }
114
InitConstProperties(napi_env env,napi_value exports)115 napi_status InitConstProperties(napi_env env, napi_value exports)
116 {
117 const napi_property_descriptor properties[] = {
118 DECLARE_NAPI_PROPERTY("Constants", ExportConstants(env)),
119 DECLARE_NAPI_PROPERTY("ValueType", ExportValueType(env)),
120 DECLARE_NAPI_PROPERTY("SyncMode", ExportSyncMode(env)),
121 DECLARE_NAPI_PROPERTY("SubscribeType", ExportSubscribeType(env)),
122 DECLARE_NAPI_PROPERTY("KVStoreType", ExportKVStoreType(env)),
123 DECLARE_NAPI_PROPERTY("SecurityLevel", ExportSecurityLevel(env)),
124 };
125 size_t count = sizeof(properties) / sizeof(properties[0]);
126
127 return napi_define_properties(env, exports, count, properties);
128 }
129 } // namespace OHOS::DistributedKVStore
130